Common advanced sorting algorithms

Source: Internet
Author: User
Tags shuffle

First, an array of max size is initialized, and the array is scrambled with the unordered function, which is used to test each sort function, with several basic sorting functions to be tested, followed by a random order function and a main dispatch function. The code is just a few lines, only the comment of the chaos accounted for a more number of rows

Quick Sort

Quick sorting, the idea of the focus is recursion + grouping (divide and conquer) + before and after cross operation Void QuickSort (int *array, int low, int hight) {//determine if the condition satisfies if (hight <= low)// If there is only one element or dislocation, it is not sorted, an element is ordered return;//satisfies the ordering condition, enters the sort part int i = low, j = hight;//The temporary variable within the function is a copy of low and hight, avoids modifying the low and hight , and then use int temp = array[low];//to filter the entire sequence, with the target value as the split point, and only when I<J indicates that the traversal is not complete, you need to continue traversing while (I < J) {while (I<j && array[j]>temp) {j--;} if (I < j) {array[i++] = array[j];} while (I < J && Array[i] < temp) {i++;} if (I < j) {array[j--] = Array[i];}} Loop out, prove i=j, traversal meet, a round of screening completed, the target number in the middle array[i] = temp;//recursive part//The first half to the fast-row function quicksort (array, low, i-1);// Give the second half to the quick-beat function quicksort (array, i + 1, hight);}

Shell sort (based on insert sort)

Shell sort (Hill sort) void Shellsort (int *array, int size, int d) {//Loop 1 control step loop for (int increment = D; increment > 0; incremen  T/= 2) {//loop 2 belongs to the Insert sort content, which controls the elements that can be accessed over successive steps for (int i = increment; i < size; i + = increment) {int temp = Array[i];int J = i- increment;//Loop 3 belongs to the Insert sort content, the assignment looks for the current element can be inserted in the position while (J >= 0 && array[j]>temp) {array[j + increment] = array[j];j- = increment;} Array[j + increment] = temp;}}}

Shell sort (sorting based on selection)

Compared to the shell sort implementation based on the insertion sort, this looks like a loop, and the logic is not simply based on the insertion sort,

Do not know is I write the question, or the problem itself is such, seek advice

void ShellSort2 (int *array, int size, int d) {//loop 1, control step change until Step 1 also executes after end for (int increment = D; increment > 0; increment /= 2) {//loop 2, find the beginning of each group for (int k = 0; k < increment; k++) {//Loop 3, which is the selection sort range, make a selection sort for (int i = k; i < size) in the group of elements provided above 1; i + = increment) {int tempindex = i;//Loop 4, belongs to select sort for (int j = i + increment; j < size; J + = increment) {if (Array[j] < A Rray[tempindex]) {tempindex = j;}} if (tempindex! = i) {int temp = Array[i];array[i] = Array[tempindex];array[tempindex] = temp;}}}}

Auxiliary operation functions, including disorderly ordering functions, printing array functions, exchanging elements worth of functions

swap function void swap (int *a, int *b) {int c = *a;*a = *b;*b = C;} The shuffle function, which iterates through the array from the back, causes the value of the current index to be exchanged with a random element that is smaller than its index (int *array, int size) {Srand ((unsigned int) (shuffle) time (NULL)); for (int i = size-1; i > 0; i--) {int index = rand ()% I;swap (&array[i], &array[index]);}} Print Array function void PrintArray (int *array, int size) {for (int i = 0; i < size; i++) {printf ("%d\t", Array[i]);}}

The main function, which is responsible for testing each sort function

int main () {//define and initialize array int Array[max] = {0};for (int i = 0; i < MAX; i++) {array[i] = i;} Sequence Shuffle (array, max) for the array data,//print out unordered arrays PrintArray (array, max),//test the effect of each sort/*printf ("\ n fast sort \ n"); QuickSort (array , 0, MAX-1);p Rintarray (array, max), */printf ("\nshell after ordering \ n"), Shellsort (array, max, MAX/2);p Rintarray (array, max); return 0;}


Common advanced sorting algorithms

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.