7 Kinds of sorting algorithms in data structure

Source: Internet
Author: User

7 Kinds of sorting algorithms in data structure

Sorting is a sequence in which any sequence of a record is rearranged into an ordered series of key values.
Time complexity mainly considers the number of elements moved.
The structure is as follows:

1. Direct Insert Sort

1, definition: Inserts each record in the sequence to be sorted sequentially into a sequence that is already ordered, until all records are ordered.
2, time complexity: In the best case, the sequence to be sorted is the positive sequence, the time complexity is O (n); In the worst case, the sequence to be sorted is reverse, the time complexity is O (n^2), and the time complexity is O (n^2) on average.
3, Space complexity: O (1).

 Public Static void Insertsort(int[] nums) {//Direct Insert Sort         for(intI=1; i<nums.length;i++) { for(intJ=i;j>0; j--) {if(nums[j]<nums[j-1]){intTEMP=NUMS[J]; nums[j]=nums[j-1]; nums[j-1]=temp; }} System. out. Print (i+":"); for(intA:nums) System. out. Print (A +" "); System. out. println (); }    }

Example array: {12,5,9,20,6,31,24}
Results:

2. Hill sort

1, Hill Sort is an improvement on the direct insertion sort.
2, definition: First, the entire sequence of records to be sorted into a number of sub-sequences, in the subsequence sequence of direct insertion, to be the whole series of records in a basic order, and then a direct insertion of the whole record to sort.
3, Time complexity: O (NLOGN) ~o (n^2).
4, Space complexity: O (1).

 Public Static void Shellsort(int[] nums) {//Hill sort        intd=nums.length/2;//Increment size         while(d>0){intk=0;//Control amount             while(k<d) {//Direct Insert Sort                 for(intI=k;i<nums.length;i=i+d) { for(intJ=i;j>0&&j-d>=0; j=j-d) {if(nums[j]<nums[j-d]) {intTEMP=NUMS[J];                            NUMS[J]=NUMS[J-D];                         Nums[j-d]=temp; }}} k++;//Increase in control volume}//whileSystem. out. Print (d+":"); for(intA:nums) System. out. Print (A +" "); System. out. println (); d=d/2; }//while}

Example array: {12,5,9,20,6,31,24}
Results:

3. Bubble sort

1, definition: 22 compares the key code of the adjacent record, and if it is reversed, it is exchanged until there is no reverse order.
2, time complexity: In the best case, the sequence to be sorted is a positive order. Its time complexity is O (n); In the worst case, the sequence to be sorted is reversed, the time complexity is O (n^2), and the average time complexity is O (n^2).
3, Space complexity: O (1).

 Public Static void Bubblesort(int[] nums) {//Bubble sort         for(inti=nums.length-1;i>0; i--) { for(intj=0; j<i;j++) {if(nums[j]>nums[j+1]){intTEMP=NUMS[J]; nums[j]=nums[j+1]; nums[j+1]=temp; }} System. out. Print (nums.length-i+":"); for(intA:nums) System. out. Print (A +" "); System. out. println (); }    }

Example array: {12,5,9,20,6,31,24}
Results:

4. Quick Sort

1, quick Sort is an improvement to the bubbling sort.
2, definition: First select an axis value, the records to be sorted into separate two parts, the left-hand record of the key is less than or equal to the axis value, the right record of the keyword is greater than or equal to the axis value, and then the two parts repeat the above process, until the whole sequence.
3, in the best case, the left sub-sequence of each axis value is the same length as the right sub-sequence, the time complexity is O (Nlogn), in the worst case, the sequence to be sorted is a positive or reverse order, the time complexity is O (n^2), and on average, the time complexity is O (NLOGN).
4, Space complexity: O (LOGN).

 Public Static void QuickSort(int[] Nums,intLow,intHigh) {//Quick Sort        if(LowintDp=partition (Nums,low,high); QuickSort (nums,low,dp-1); QuickSort (nums,dp+1, high); }Else{return; }           } Public Static int Partition(int[] Nums,intLow,intHigh) {intPivot=nums[low]; while(Low while(Low < high && nums[high]>=pivot) high--; Nums[low]=nums[high]; while(Low < high && nums[low]<=pivot) low++;        Nums[high]=nums[low]; } Nums[low]=pivot;//At this time low equals high, so, can also be written nums[high]=pivot;        returnLow//At this time low equals high, so return any one is correct}

Example array: {12,5,9,20,6,31,24}
Results:

5. Simple selection of sorting

1, definition: I pass through the comparison of the N-i sub-key code, in N-i-1 (1<=i<=n-1) records to select the minimum key code record, and the first record exchange as an ordinal sequence of the first I records.
2, Time complexity: Best, worst, average time complexity is O (n^2).
3, Space complexity: O (1).

         Public Static void Selectsort(int[] nums) {//Simple Select sort         for(intI=0; i<nums.length-1; i++) {intSindex=i;//Minimum number subscript            intSnum=nums[i];//Minimum number size             for(intj=i+1; j<nums.length;j++) {if(Nums[j]<snum)                    {sindex=j;                SNUM=NUMS[J]; }            }//Exchange            intTemp=nums[i];            Nums[i]=nums[sindex];            Nums[sindex]=temp; System. out. Print (i+1+":"); for(intA:nums) System. out. Print (A +" "); System. out. println (); }    }

Example array: {12,5,9,20,6,31,24}
Results:

6. Heap Sequencing

1, heap sorting is an improvement on simple selection sorting.
2, first the sequence of records to be sorted into a heap, at this time, the largest of all records in the heap is the top record of the heap, and then removed from the heap, and the rest of the records are adjusted piles, so that the second largest record, and so on, until there is only one record in the heap.
3, Time complexity: Best, worst, average time complexity is O (NLOGN).
4, Space complexity: O (NLOGN).

 Public Static void Heapsort(int[] nums) {//Heap sort    if(Nums = =NULL|| Nums.length <=1) {return; } buildmaxheap (Nums);//Call the function to build the heap    //Adjust the top element of the heap to the end of the array, and then continue resizing the current heap to a large top heap     for(inti = nums.length-1; I >=1; i--) {inttemp=nums[0]; nums[0]=nums[i];        Nums[i]=temp; Maxheap (Nums, I,0); System. out. Print (nums.length-i+":"); for(intA:nums) System. out. Print (A +" "); System. out. println (); }}Private Static void Buildmaxheap(int[] nums) {//Build heap    if(Nums = =NULL|| Nums.length <=1) {return; }intHalf = nums.length/2; for(inti = half; I >=0;    i--) {maxheap (nums, nums.length, i); }}Private Static void Maxheap(int[] Nums,intHeapSize,intIndex) {//recursive adjustment for large top heap    intLeft = index *2+1;intright = Index *2+2;if(Left > HeapSize && right > HeapSize) {//Do not have this return, is also correct, OK! I didn't read it.           return; }intlargest = index;if(Left < heapsize && Nums[left] > Nums[index])    {largest = left; }if(Right < HeapSize && Nums[right] >nums[largest])    {largest = right; }if(Index! = largest) {intTemp=nums[index];        Nums[index]=nums[largest];        Nums[largest]=temp;    Maxheap (Nums, heapsize, largest); }}

Example array: {12,5,9,20,6,31,24}
Results:
Code Reference Links

7. Two-Way merge sort

1, definition: Several sequential sequences are 22 merged until all pending records are in an ordered sequence.
2, Time complexity: Best, worst, average O (Nlogn).
3, Space complexity: O (n).

  Public Static void MergeSort(int[] A,intLeftintright) {if(Left<right) {//int Center = (left + right) >> 1;           intCenter= (left+right)/2;           MergeSort (A, left, center); MergeSort (A, center +1, right);        Merge (A, left, center, right); }Else{return; }} Public Static void Merge(int[] Data,intLeftintCenterintright) {int[] Tmparr =New int[right+1];intMID = center +1;intindex = left;Index of the temp array for the//index record        intTMP = left;//Remove the smallest put-in temporary array from two arrays         while(Left <= Center && mid <= right)        {tmparr[index++] = (Data[left] <= data[mid])? data[left++]: data[mid++]; }//The remainder is put into a temporary array in turn         while(Mid <= right)        {tmparr[index++] = data[mid++]; } while(Left <= Center)        {tmparr[index++] = data[left++]; }//Copy the contents of the temporary array back to the original array         for(inti = tmp; I <= right;        i++) {Data[i] = Tmparr[i]; } System. out. println (arrays.tostring (data)); } Public Static void Main(string[] args) {int[] arr={ A,5,9, -,6, to, -}; MergeSort (arr,0, arr.length-1); }

Example array: {12,5,9,20,6,31,24}
Results:
Code Reference Links

7 Kinds of sorting algorithms in data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.