The implementation and efficiency analysis of package edu.cn.ysw;//eight sorting algorithms * * Sort in * * 1. Insert Sort: Direct Insert sort, hill sort. * 2. Select sort: Simple select sort, heap sort. 3. Swap sort: bubble sort, quick sort. 4. Merge sort 5. Base sort */public class Sortedmethods {/** * @author YSW * @param args * @throws Exception * @since 6/15/2017 *///Direct Insertion sort: 1. With Sentinel, 2. No Sentinel//No Sentinel public void Insertsortwithoutpre (int arr[]) {if (arr = = NULL | | arr.length = = 0) {return;} The outer loop defines the number of sorts for (int i = 1; i < arr.length; i++) {//does not meet the criteria and ignores this judgment, equivalent to Continue;if (Arr[i] < arr[i-1]) {int TempVal UE = Arr[i];int j;for (j = i-1; J >= 0 && Tempvalue < arr[j]; j--) {arr[j + 1] = arr[j];} Arr[j + 1] = Tempvalue;}}} There is a direct insertion of the Sentinel sort public void Insertsortwithpre (int arr[]) {for (int i = 2; i < arr.length; i++) {/* * Sentinel's effect: 1: Save the number waiting to be inserted; 2 : Prevents the array from crossing the bounds of the case; */if (Arr[i] < arr[i-1]) {arr[0] = Arr[i];int j;for (j = i-1; Arr[0] < arr[j]; j--) {arr[j + 1] = arr[j];} Arr[j + 1] = arr[0];}}} Hill sort public void shellsort (int arr[]) {if (arr = = NULL | | arr.length = = 0) {return;} Increment sequence int incrementvalue = ARR.LENGTH/2;WHIle (Incrementvalue >= 1) {for (int i = 0, i < arr.length; i++) {for (int j = i + Incrementvalue; j < Arr.length; J + = incrementvalue) {int temp = Arr[j];int k;for (k = j-incrementvalue; k >= 0 && arr[k] > temp; k = k Incrementvalue) {arr[k + incrementvalue] = arr[k];} Arr[k + incrementvalue] = temp;}} Change the value of the increment sequence Incrementvalue/= 2;}} /* Select Sort: 1. Simple selection sort 2. heap sort *///Simple select sort public void Selectsort (int arr[]) {for (int i = 0; i < arr.length-1; i++) {for ( Int J = i + 1; J < Arr.length; J + +) {if (Arr[i] > Arr[j]) {swap (arr, I, j);}}} Heap sequencing: In practice, many of the scheduling algorithms, such as thread priority scheduling//Time-driven: Take the minimum or the longest waiting time//bubble sort public void bubblesort (int arr[]) {for (int i = 0; i < arr.length -1; i++) {for (int j = 0; J < arr.length-1-I; j + +) {if (Arr[j] > arr[j + 1]) {swap (arr, J, j + 1);}}} Quick sort: Dig pit fill + divide//Quick Sort returns the function of the final position of the Datum value partition: The position of the base number after the adjustment back to public int partition (int arr[], int l, int r) {//Assuming the first datum value is Arr[0];i NT left = L;int right = R;int key = Arr[left];while (Left < right) {WhilE (Left < right && Arr[right] >= key) {right--;} if (left < right) {Arr[left] = arr[right];left++;} while (left < right && Arr[left] <= key) {left++;} if (left < right) {arr[right] = arr[left];right--;}} Arr[left] = Key;return left;} Quick sort public void quickSort (int arr[], int l, int r) {if (L < r) {int index = partition (arr, L, R); QuickSort (arr, L, I NDEX-1); QuickSort (arr, index + 1, r);}} Merge sort public void mergesort (int arr[]) {sort (arr, 0, arr.length-1);} public void sort (int[] arr, int left, int. right) {if (left >= right) {return;} int center = (left + right)/2;sort (arr, left, center), sort (arr, center + 1, right), merge (arr, left, center, right);} public void merge (int[] arr, int left, int center, int right) {//define a secondary array to hold the merged two subarray int temparray[] = new Int[arr.length] ;//Left face array first element int leftfirst = left;//Right face array first element int rightfirst = center + 1;//point to the first element of the auxiliary array int temppointer = Left;while (Leftfirst <= Center && rightfirst <= right) {if (Arr[leftfirst] <= Arr[rightfirst]) {temparray[temppointer++] = arr[leftfirst++];} else {temparray[temppointer++ ] = arr[rightfirst++];}} Adds the remaining elements in the first array to the secondary array while (Leftfirst <= center) {temparray[temppointer++] = arr[leftfirst++];} Add the remaining elements in the second array to the secondary array while (Rightfirst <= right) {temparray[temppointer++] = arr[rightfirst++];}} The function of the print array is public void Printarr (int arr[]) {for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i]) and if (I! = arr.). length-1) {System.out.print (",");}} System.out.println ();} Interchange elements of the array public void swap (int arr[], int i, int j) {int temp = Arr[i];arr[i] = arr[j];arr[j] = temp;} public static void Main (string[] args) {sortedmethods smethods = new Sortedmethods ();//test required array int array1[] = {1, 2, 3, 4 , 5};int array2[] = {5, 4, 3, 2, 1};int array3[] = {4, 5, 2, 1, 7};int array4[] = null;int array5[] = new int[0];//test Try no Sentinel insertion sort System.out.println ("--------have sentinel insertion sort--------------"); Smethods.insertsortwithoutpre (array1); Smethods.printarr (array1); SMetHods.insertsortwithoutpre (array2); Smethods.printarr (array2); Smethods.insertsortwithoutpre (ARRAY3); Smethods.printarr (ARRAY3);/* * SMETHODS.INSERTSORTWITHOUTPRE (ARRAY4); Smethods.printarr (ARRAY4); * SMETHODS.INSERTSORTWITHOUTPRE (ARRAY5); Smethods.printarr (ARRAY5); */system.out.println ("--------No sentinel Insertion sort------------");//test with Sentinel Insertion sort smethods.insertsortwithoutpre (array1); Smethods.printarr (array1); Smethods.insertsortwithoutpre (array2); Smethods.printarr (array2); Smethods.insertsortwithoutpre (ARRAY3); Smethods.printarr (ARRAY3); System.out.println ("----------Hill sort------------");//test Hill Sort Smethods.shellsort (array1); Smethods.printarr (array1); Smethods.shellsort (array2); Smethods.printarr (array2); Smethods.shellsort (ARRAY3); Smethods.printarr (ARRAY3); System.out.println ("---------Simple selection sort------------"); Smethods.selectsort (array1); Smethods.printarr (array1); Smethods.selectsort (array2); Smethods.printarr (array2); Smethods.selectsort (ARRAY3); Smethods.printarr (ARRAY3); System.out.println ("---------bubble sort------------"), SMeThods.bubblesort (array1); Smethods.printarr (array1); Smethods.bubblesort (array2); Smethods.printarr (array2); Smethods.bubblesort (ARRAY3); Smethods.printarr (ARRAY3); System.out.println ("---------Quick sort------------"); Smethods.quicksort (array3, 0, array3.length-1); Smethods.printarr (ARRAY3); System.out.println ("---------merge sort------------"); Smethods.mergesort (ARRAY3); Smethods.printarr (ARRAY3);}}
Java implementation of six kinds of common sorting algorithms