The basic idea of choosing a sort is: every trip from n-i+1 (i=1,2,..., N) elements, select the element with the smallest keyword as the element i in an ordered sequence. This section, based on the introduction of simple selection sorting, gives an improved algorithm heap ordering.
1. Simple selection sort A: Algorithm description
Simple selection of the basic idea of sorting is very simple, namely: first trip, from n elements to find the smallest element of the keyword exchange with the first element; the second, the element with the smallest key in the n-1 element starting from the second element is exchanged with the second element; So, K-Pass, the n-k+ starting from the K element The element with the smallest key selected in 1 elements is exchanged with the K element until the entire sequence is ordered by the keyword.
B: Algorithm implementation
public void Selectsort (int[] r, Int. Low, int.) {for (int k = low; k < high-1; k++) {//Make n-1 trip Select int min = k;for (i NT i = min + 1; I <= high; i++)//Select the smallest element of the keyword if (compare (R[i], r[min]) min = i;if (k! = min) {int temp = r[k];//keyword smallest element with element R[k] Interchange r[k] = R[min];r[min ] = temp;} End of if}//end of for (int k=0 ...} End of Selectsort
"Efficiency analysis"
Space efficiency: Obviously simply selecting a sort requires only one secondary space.
Time efficiency: In the simple selection of the order, the number of elements required to move less, in order to sort the sequence has been ordered, the simple selection of the order does not need to move the element, in the worst case, that is to be sorted sequence itself is reversed, the number of times to move the element is 3 (n-1). However, regardless of the number of times the element is moved during the simple selection process, in any case, a simple select sort requires n (n-1)/2 comparison operations, so the time complexity for simple selection sorting is 0 (n²)
C: algorithm Example
Selectsort.java
Package Com.test.sort.selection;public class Selectsort {/** * @param args */public static void main (string[] args) {//To Do auto-generated method StubSystem.out.println ("Simple Select sort sort function Implementation" ""); int[] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; Selectsort sort = new Selectsort (); SYSTEM.OUT.PRINTLN ("Sequence before sequencing:"); Sort.printarr (arr); Sort.selectsort (arr, 0, arr.length-1); System.out.println ("Sorted after sequence:");; Sort.printarr (arr);} public void Selectsort (int[] r, Int. Low, int.) {for (int k = low; k < high-1; k++) {//Make n-1 trip Select int min = k;for (i NT i = min + 1; I <= high; i++)//Select the smallest element of the keyword if (compare (R[i], r[min]) min = i;if (k! = min) {int temp = r[k];//keyword smallest element with element R[k] Interchange r[k] = R[min];r[min ] = temp;} End of if}//end of for (int k=0 ...} End of Selectsortpublic boolean compare (int parama, int paramb) {if (Parama < Paramb) {return true;} else {return FA LSE;}} /** * Prints an array of elements */public void Printarr (int[] arr) {if (arr! = null) {for (int temp:arr) {System.out.print (temp + ""); }system.out. println ();}}}
D: Result output
2. Heap sort A: Algorithm description
Have n elements that you want to sort by keyword. These n elements can be first built into the heap by keyword, and the top element of the heap is output, resulting in the largest (or smallest) element of the keyword in n elements. Then, the remaining n-1 elements are re-built into the heap, and then the top elements of the heap are exported, and the elements of the n elements are large (or minor). So repeatedly, until there is only one element left, you can get an ordered sequence, which is called a heap sort. Heap ordering solves two problems, one is how to build the sequence of n elements into a heap of keywords, and one is the output heap top element, how to adjust the remaining n-1 elements so that they become a new heap by keyword.
Adjusting the heap structure
Initial heap Build process
B: Algorithm implementation
public void Heapsort (int[] r) {int n = r.length-1;for (int i = N/2; I >= 1; i--)//Initialize Build heap Heapadjust (R, I, n); for (in t i = n; i > 1; i--) {//continuously output heap top element and adjust r[1..i-1] to new heap int temp = r[1];//Exchange heap top and base element r[1] = R[i];r[i] = Temp;heapadjust (r, 1, i-1);//Adjustment}}//has Know R[low. High] except R[low], the rest of the elements satisfy the definition of the heap private void Heapadjust (int[] r, int low, int.) {int temp = r[low];for (int j = 2 * low; J <= High; j = J * 2) {//filter downwards along key elements//J points to the larger key element if (J < high && compare (R[j], r[j + 1])) j++;//If temp is larger than its child, insert to low The indicated position if (!compare (temp, r[j])) Break;r[low] = R[j];low = j; Filter down}r[low] = temp;}
C: algorithm Example
Heapsort.java
Package Com.test.sort.selection;public class Heapsort {/** * @param args */public static void main (string[] args) {//TODO Auto-generated method StubSystem.out.println ("Heap sorting Function Implementation" ""); int[] arr = {1, 54, 6, 1, 65, 34, 2, 67, 7, 9, 43}; Heapsort sort = new Heapsort (); SYSTEM.OUT.PRINTLN ("Sequence before sequencing:"); Sort.printarr (arr); Sort.heapsort (arr); System.out.println ("Sorted after sequence:"); Sort.printarr (arr);} public void Heapsort (int[] r) {int n = r.length-1;for (int i = N/2; I >= 1; i--)//Initialize Build heap Heapadjust (R, I, n); for (in t i = n; i > 1; i--) {//continuously output heap top element and adjust r[1..i-1] to new heap int temp = r[1];//Exchange heap top and base element r[1] = R[i];r[i] = Temp;heapadjust (r, 1, i-1);//Adjustment}}//has Know R[low. High] except R[low], the rest of the elements satisfy the definition of the heap private void Heapadjust (int[] r, int low, int.) {int temp = r[low];for (int j = 2 * low; J <= High; j = J * 2) {//filter downwards along key elements//J points to the larger key element if (J < high && compare (R[j], r[j + 1])) j++;//If temp is larger than its child, insert to low The indicated position if (!compare (temp, r[j])) Break;r[low] = R[j];low = j; Filter down}r[low] = temp;} Public Boolean compare (int parama, int paramb) {if (Parama < Paramb) {return true;} else {return false;}} /** * Prints an array of elements */public void Printarr (int[] arr) {if (arr! = null) {for (int temp:arr) {System.out.print (temp + ""); }system.out.println ();}}}
D: Result output
Sequencing of data structures and algorithms (summed up three)