实验八--排序算法

实验八 排序算法的实现

一、 实验目的:

  1. 熟练掌握常用的直接插入排序、简单选择排序、冒泡排序等算法。
  2. 深入理解各种查找排序的结构特点及各算法之间的区别。

    二、 实验内容:

  3. 采用直接插入排序、简单选择排序、冒泡排序等常用的排序算法实现给数据的排序。
  4. 各种排序算法的结构特点及各算法之间的区别。

    代码如下:

    RecordNode.java

    package sort;
    import java.util.Scanner;
    public class RecordNode
    {
    public int key  ;  // 关键字
    public String element;   // 记录的其它部分 
    
    Scanner sin = new Scanner(System.in);
    public RecordNode()
    {
        System.out.println("请输入关键字:");
        this.key=sin.nextInt();
        System.out.println("请输入元素值:");
        this.element=sin.next();
    }
    
    public RecordNode(int key)
    {
        this.key =key;
    }
    
    public RecordNode(int key , String element)
    {
        this.element = element;
        this.key =key;
    }
    
    public String toString()
    {
        return this.key+" "+this.element+" ";
    }
    }

    SeqList_Sort.java(主要填写算法部分)

    /*
     *构造用于排序的线性表,
     *元素位置从下标1开始
     *分别设置各种不同的排序算法*/
    package sort;
    import java.util.Scanner;
    import java.util.Random;
    public class SeqList_Sort
    {
    public RecordNode [ ] r;
    public int curlen;
    
    public SeqList_Sort(int maxSize) //构造方法,用参数做为线性表的初始空间大小
    {
        this.curlen=0;
        this.r = new RecordNode[maxSize];
    }
    
    public SeqList_Sort()  //构造方法,以默认100做为初始空间大小
    {
        this(100);
    }
    
    public void create(int n)  // 输入生成线性表,参数为输入元素个数
    {
        Scanner sin =new Scanner(System.in);
        System.out.println("\t输入"+n+"个线性表的元素:");
        for(int i=1;i<=n;i++)
        {
            try {
                        RecordNode newdata=new   RecordNode  ();
                        this.insert(i,newdata);
                  }
            catch(Exception e)
            {
                System.out.println("\t插入位置不合适,无法创建");
            }
        }
    }
    
    public void create( )// 随机数生成10个元素
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("\t输入元素个数:  ");
        int  n = sc.nextInt();
        for(int i=1;i<=n ;i++)
        {
            try {
                Random rd = new Random();
                RecordNode newdata = new RecordNode(rd.nextInt(100)," ");
                this.insert(i,newdata);
                }
            catch(Exception e)
            {
                System.out.println("\t插入位置不合适,无法创建");
            }
        }
    }
    
    public void clear()//清空
    {
        this.curlen=0;
    }
    
    public boolean isEmpty() //判断是否为空
    {
        if ( this.curlen ==0 )
            return true;
        else
            return false;
    }
    
    public int length() //求长度
    {
        return this.curlen;
    }
    
    public RecordNode get(int i) throws Exception //求指定位置元素
    {
        if ( i < this.curlen)
           return  this.r[i];
        else
        {
            throw new Exception("elem no."+i+" not exist!");
        }
    }
    
    public void insert(int i,RecordNode x)throws Exception //在指定的位置上插入数据元素
    {
        if (this.curlen == this.r.length)
        {
        //  throw new Exception("overflow!");
            RecordNode newlist[]=new RecordNode[2*this.curlen];
            for(int j=this.curlen;j>0;j--)
            {
                newlist[j]=this.r[j];
            }
            this.r=newlist;
        }
        if (i<1|| i>this.curlen+1)
            throw new Exception("position  error!");
        for(int j =this.curlen ; j >=i ; j-- )
        {
            this.r[ j+1] =this.r[j] ;
        }
        this.r[i]= x;
        this.curlen++;
    }   
    
    public void remove(int i) //删除指定位置上的元素
    {
        for (int  j = i+1 ; j<= this.curlen ; j++)
        {
            this.r[j-1]=this.r[j];
        }
        this.curlen--;
    }   
    
    public int indexOf(RecordNode x)//查找指定元素的位置
    {
         int i;
         for( i=this.curlen; i>0 ; i--)
         {
            if (this.r[i].equals(x))
                     break;
         }
         return i;
    }
    
    public void display() //输出线性表的所有元素值
    {
         System.out.println("\n\t线性表的元素是");
         for (int i = 1; i <= curlen ;i++)
           {
                System.out.print("\t"+this.r[i] );
                if (i %10==0)
                    System.out.println();
           }
        System.out.println();
    } 
    
    //---------------------Sort---------------------------
    //------------------insertSort-----------------------
    public void insertSort() // 插入排序
    {
        RecordNode temp;
        int i,j;
        for(i = 1;i <= this.curlen; i++)
        {
            temp = r[i];
            for(j = i-1;j>=0&&Integer.valueOf(temp.key).compareTo(r[j].key)<0;j--)
            {
                r[j+1]= r[j];
            }
            r[j+1]=temp;
        }
    }
    
    //----------------带岗哨的插入排序-------------
    public void insertSortWithGuard() // 带岗哨的插入排序
    {
       int i,j;
       for(i = 1;i<=this.curlen;i++)
       {
        r[0] =r[i];
        for(j = i-1;Integer.valueOf(r[0].key).compareTo(r[j].key)<0;j--)
        {
            r[j+1]=r[j];
        }
        r[j+1] = r[0];
       }
    }   
    
    //----------------选择排序-------------
    public void  selectSort()  //选择排序
    {
        RecordNode temp;
        for(int i = 0 ; i <= this.curlen ;i++)
        {
            int min = i;
            for(int j = i+1;j<=this.curlen;j++)
            {
                if(Integer.valueOf(r[j].key).compareTo(r[min].key)<0)
                {
                    min = j;
                }
            }
            if(min!=i)
            {
                temp = r[i];
                r[i] = r[min];
                r[min]=temp;
            }
        }
    }
    
    //----------------冒泡排序-------------
    public void bubbleSort() //冒泡排序
    {
        RecordNode temp;
        boolean flag = true;
        for(int i = 1;i<= this.curlen&&flag;i++)
        {
            flag = false;
            for(int j = 0;j<=this.curlen-i;j++)
            {
                if(Integer.valueOf(r[j].key).compareTo(r[j+1].key)>0)
                {
                    temp = r[j];
                    r[j]=r[j+1];
                    r[j+1]= temp;
                    flag = true;
                }
            }
        }
    }
    
    //----------------快速排序-------------
    private int Partition(int i,int j) // 在区间[i,j]上进行元素的划分,以i位置上的元素为支点记录
    {
        RecordNode pivot = r[i];
        while(i<j)
        {
            while(i<j&&Integer.valueOf(pivot.key).compareTo(r[j].key)<=0)
            {
                j--;
            }
            if(i<j)
            {
                r[i]=r[j];
                i++;
            }
            while(i<j&&Integer.valueOf(pivot.key).compareTo(r[i].key)>0)
            {
                i++;
            }
            if(i<j)
            {
                r[j] = r[i];
                j--;
            }
        }
        r[i]=pivot;
        return i;
    }
    
    private void qSort(int low,int high)  //从low到high进行递归的快速排序
    {
        if(low < high)
        {
            int p = Partition(low,high);
            qSort(low,p-1);
            qSort(p+1,high);
        }
    }
    
    public void quickSort() // 快速排序
    {
        qSort(1,this.curlen);
    }
    
    //----------------堆排序-------------
    public void heapSort() // 堆排序
    {
        int n=this.curlen;
        for(int i=n/2; i>=1; i--) // 建堆
        {
                sift(i,n);
        }
    
        for(int i=n;  i>=2; i--) // 利用堆排序
        {
                //交换r[1]和r[i],即得到最大值r[i]
               RecordNode temp = this.r[1];
                this.r[1] = this.r[i];
                this.r[i] = temp;
                sift(1,i);
        }
    }
    
    private void sift(int low,int high) // 以low为根的子树调整成大根堆
    {
        if (low <high)
        {
            int i=low;
            int j=2*i ;
            this.r[0] = this.r[i];
            while( j  <  high)
            {
                    if (j<high-1 && this.r[j].key < this.r[j+1].key) // i有右孩子,并且右孩子大于左孩子,j将表示右孩子
                            j++;
                    if (this.r[0].key < this.r[j].key)
                    {
                            this.r[i] = this.r[j];
                            i=j;
                            j=2*i;
                    }
                    else
                            j=high +1; // 可以退出调整过程
            }
            this.r[i]=this.r[0];
        }
    }
    }

    Sort_Test.java

    import sort.SeqList_Sort;
    import java.util.Scanner;
    public class Sort_Test
    {
    private static int total;  //排序次数
    
    private static void showMenu()
    {
        System.out.println("    ---------Menu--------");
        System.out.println("    1  insertSort");
        System.out.println("    2  insertSortWithGuard ");
        System.out.println("    3  bubbleSort ");
        System.out.println("    4  quickSort ");
        System.out.println("    5  selectSort ");
        System.out.println("    6  heapSort ");
        System.out.println("    0  Quit  ");
        System.out.println("    ---------end----------");
    }
    
    public static void main(String [] args)
    {
        Scanner sin = new Scanner(System.in);
        SeqList_Sort  test = new SeqList_Sort ();
        do
        {
    
            showMenu();
            System.out.print("  请选择排序算法:");
            int choice = sin.nextInt();
            total++;
    
            if (choice == 0 )
                        System.exit(0);
    
            System.out.println("    初始数据:");
            test.create();
            test.display();
    
            if (choice >=1 && choice <=6)
            {
                String sortName = null;
                        switch(choice)
                        {
                                case 1: test.insertSort(); sortName = "插入排序" ; break;
                                case 2: test.insertSortWithGuard();  sortName = "带岗哨的插入排序" ; break;
                                case 3: test.bubbleSort();  sortName = "冒泡排序";break;
                                case 4: test.quickSort();  sortName = "快速排序" ;break;
                                case 5: test.selectSort(); sortName = "选择排序" ; break;
                                case 6: test.heapSort();  sortName = "堆排序" ;break;
                        }
                       System.out.println("**第  "+total+"  次选择,"+sortName+"结果是:");
                       test.display();
              }
              else
                       System.out.println("error,end");
    
            System.out.println("_______请继续选择_________");
    
            test.clear();  //清除上次数据
        }while(true);
    }
    }

时间: 2024-08-11 04:21:07

实验八--排序算法的相关文章

八排序算法

nO(nlog2n) keyword 1.(Straight Insertion Sort) : 112 void print(int a[], int n ,int i){ cout<<i <<":"; for(int j= 0; j<8; j++){ cout<<a[j] <<" "; } cout<<endl; } void InsertSort(int a[], int n) { for(in

八种排序算法

最近一段时间自己在研究各种排序算法,于是自己写了一个八种排序算法的集合: /************************************************************************* > Copyright (c)2014 stay hungry,stay foolish !!! > File Name: sort.cpp > Author: kanty > Mail: [email protected] > Created Time:

八种排序算法(内部排序)

八种排序算法很长时间没有使用了,今天做一个总结,方便以后自己用的时候参考. 这八种排序算法都是内部算法,这八种排序算法分别是: 1. 插入排序 1)直接插入排序 2)希尔排序 2.选择排序 1)简单选择排序 2)堆排序 3.交换排序 1)冒泡排序 2)快速排序 4.归并排序 5.基数排序 一.直接插入排序 将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表.在实际操作中,先将序列的第一个记录看成是一个有序的子序列,然后从第二个.第三个.……记录逐个进行插入,直至整个序列有

八种排序算法效率比较

从刚上大一那会儿学的C语言开始,就已经接触到了不少排序算法,但当时都只是为了完成简单的排序任务而已,而且所给的数据也不够多,所以看不出各个排序算法间的执行效率的优劣.最近有个数据结构课程设计的实验,是有关于排序算法之间的效率比较,我就顺便把它放上来了,并对各个算法执行的效率时间做了柱形统计图表.此次实验主要测试了8种排序算法:插入排序.快速排序.冒泡排序.希尔排序.简单选择排序.堆排序.归并排序.折半插入排序. 总共建立了三种情况,分别是平均排序.最好情况排序.最坏情况排序.第一种情况就是使用了

数据结构实验之排序八:快速排序

数据结构实验之排序八:快速排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定N(N≤10^5)个整数,要求用快速排序对数据进行升序排列,注意不得使用STL. Input 连续输入多组数据,每组输入数据第一行给出正整数N(≤10^5),随后给出N个整数,数字间以空格分隔. Output 输出排序后的结果,数字间以一个空格间隔,行末不得有多余空格. Example Input 8 49

复习数据结构:排序算法(八)——基排序

基排序是外排序,稳定的排序算法. 它的时间复杂度:O(d(r+n)),d为每个数的长度,r表示基数,n表示数组中元素的个数. 基数排序是另外一种比较有特色的排序方式,它是怎么排序的呢?我们可以按照下面的一组数字做出说明:12. 104. 13. 7. 9 (1)按个位数排序是12.13.104.7.9 (2)再根据十位排序104.7.9.12.13 (3)再根据百位排序7.9.12.13.104 这里注意,如果在某一位的数字相同,那么排序结果要根据上一轮的数组确定,举个例子来说:07和09在十分

八种常用的排序算法(转)

下面要讲到的8种排序都属于内部排序,既在内存中完成,主要从理论原理方面来分析的.    插入排序 ①直接插入排序 例:六个数12 15 9 20  6 31 24 用直接插入排序,如下图: 思路: 第一步:从给出的六个数中,随便拿出一个数,比如12,形成一个有序的数据序列(一个数当然是有序的数据序列了,不看12之外的数,就当其他的数不存在): 第二步:从剩下的五个数中挑出一个数来,比如15,和刚才的12作比较,12<15,因此,放在12后面,形成数据序列12 15: 第三步:从剩下的四个数中挑出

八大排序算法的python实现(八)简单选择排序

代码: #coding:utf-8 #author:徐卜灵 # L = [6, 3, 2, 32, 5, 4] def Select_sort(L): for i in range(0,len(L)): for j in range(i,len(L)): if L[i] > L[j]: #打擂台的形式 # temp = L[i] # L[i] = L[j] # L[j] = temp # L[i],L[j] = L[j],L[i] return L print Select_sort(L) 这个

数据结构之排序算法(八大排序)-(八)

排序算法可以分为稳定排序和不稳定排序.在简单形式化一下,如果A[i] = A[j],A[i]原来在位置前,排序后A[i]还是要在A[j]位置前,这才能叫稳定排序.排序算法如果是稳定的,那么从一个键上排序,然后再从另一个键上排序,第一个键排序的结果可以为第二个键排序所用.基数排序就是这样,先按低位排序,逐次按高位排序,低位相同的元素其顺序再高位也相同时是不会改变的.另外,如果排序算法稳定,对基于比较的排序算法而言,元素交换的次数可能会少一些(个人感觉,没有证实). 回到主题,现在分析一下常见的排序