The structural characteristics of various sorting algorithms and the differences between the algorithms.
The code is as follows:
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 (main filling algorithm section)
/* Constructs a linear table for sorting, * element position starting from Subscript 1 * Set various sorting algorithms separately */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)//construction method, with parameters as the initial space size of the linear table { this.curlen=0; THIS.R = new Recordnode[maxsize];} Public Seqlist_sort ()//construction method, with default 100 as the initial space size {this (100);} public void Create (int n)//input to generate a linear table with parameters of input elements {Scanner sin =new Scanner (system.in); System.out.println ("\ t input" +n+ "elements of a linear table:"); for (int i=1;i<=n;i++) {try {Recordnode newdata=new recordnode (); This.insert (I,newdata); } catch (Exception e) {System.out.println ("\ t insertion position is inappropriate and cannot be created"); }}}public void Create ()//random number generates 10 elements {Scanner sc = new Scanner (system.in); System.out.print ("\ t INPUT element number:"); 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 insertion position is inappropriate and cannot be created"); }}}public void Clear ()//empty {this.curlen=0;} public boolean isEmpty ()//Determines whether null {if (This.curlen ==0) return true; else return false;} public int Length ()//Request length {return this.curlen;} Public Recordnode get (int i) throws Exception//ask for the specified position element {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//Insert Data element {if (This.curlen = = this.r.length) at specified position {//T Hrow 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)//delete the element at the specified position {for (int j = i+1; j<= This.curlen; j + +) {This.r[j-1]=this. R[J]; } this.curlen--;} public int indexOf (Recordnode x)//Find the position of the specified element {int i; for (I=this.curlen; i>0; i--) {if (This.r[i].equals (x)) break; } return i; }public void Display ()//all element values of the output linear table {System.out.println ("Elements of the \n\t linear table are"); 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 ()//insert sort {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; }}//----------------Insertion Sort with sentry-------------public void Insertsortwithguard ()//insertion sort with Sentry {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]; }}//----------------Select sort-------------public void selectsort ()//select sort {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; }}}//----------------bubble sort-------------public void bubblesort ()//bubble sort {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; }}}}//----------------quick sort-------------private int Partition (int i,int j)//Divide the element on the interval [i,j], with the element on the I position as pivot record { 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)//from low to highA quick sort of recursion {if (Low < high) {int p = Partition (Low,high); QSort (low,p-1); QSort (P+1,high); }}public void QuickSort ()//Quick Sort {qSort (1,this.curlen);} ----------------Heap sort-------------public void heapsort ()//heap sort {int n=this.curlen; for (int i=n/2; i>=1; i--)//Build Heap {sift (i,n); } for (int i=n; i>=2; i--)//Use heap Sort {//interchange r[1] and R[i], that is, get maximum 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)//sub-tree with low root is adjusted to large root heap {if (low
Sort_test.java
Import sort. Seqlist_sort;import Java.util.scanner;public class sort_test{private static int total; Number of sorts 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 ("Please select the sorting algorithm:"); int choice = Sin.nextint (); total++; if (choice = = 0) system.exit (0); SYSTEM.OUT.PRINTLN ("Initial data:"); Test.create (); Test.display (); if (Choice >=1 && Choice <=6) {String sortname = null; Switch (choice) {case 1:test.insertsort (); sortname = "Insert sort"; break; Case 2:test.insertsortwithguard (); Sortname = "Insertion order with sentry"; Break Case 3:test.bubblesort (); Sortname = "bubble sort"; Case 4:test.quicksort (); Sortname = "quick sort"; Case 5:test.selectsort (); Sortname = "Select Sort"; Break Case 6:test.heapsort (); Sortname = "heap sort"; } System.out.println ("* *" +total+ "sub-selection," +sortname+ "The result is:"); Test.display (); } else System.out.println ("error,end"); SYSTEM.OUT.PRINTLN ("_______ Please continue to select _________"); Test.clear (); Clears the last data}while (TRUE);}}