Using split-treatment method to realize quick-row

Source: Internet
Author: User

#include "stdio.h"

/*

* Display Array

* Only the data from begin to end subscript is displayed to the corresponding location.

* For example, the array is 13, 17, 12 and executes successively:

* Show (array, 3, 0, 2);

* Show (array, 3, 1, 2);

* Show (array, 3, 1, 1);

* will appear as:

* 13 17 12

* 17 12

* 17

*/

void Show (int array[], long maxlen, int begin, int end)

{

int i = 0;


/* Data not cared for is populated with spaces */

for (i = 0; i<begin; i++)

printf ("");


for (i = begin; I <= end; i++)

printf ("%4d", Array[i]);


printf ("\ n");

}

/*

* Two number exchange values.

* Returned as 1 when the description was exchanged.

* Return as 0 indicates two values are the same no need to swap.

* This return value can be used in situations where you want to print the results only.

*/

int swap (int *i, int *j)

{

int temp;


if (*i = = *j)

return 0;


temp = *i;

*i = *j;

*j = temp;


return 1;

}


/*

* Quick Sort function

* This function only sorts the data between the begin subscript (including begin) and end (including end)

*/

void quicksort (int array[], int maxlen, int begin, int end)

{

int I, J;


if (begin < End) {

/* Since the starting value does not move as a datum, the value to be compared is starting with begin+1 */

i = begin + 1;

j = END;


/*

* The function of this cycle: Array[begin] for the reference to array[begin+1] and Array[end]

* The number of the first group between the group, after the effect of the post-cycle description

*

* Write here while (i! = j) is equivalent because the initial state is i<=j and each time I and J

* The relative change value is 1 so there is no case of i>j

*/

while (I < j) {

/* If the current value is greater than array[begin], change the current value to the position where j is pointing, and the post-shift J subscript moves forward */

if (Array[i] > Array[begin]) {

if (Swap (&array[i], &array[j]) = = 1)

Show (array, maxlen, begin, end); /* Show only data between Begin and end subscripts */

j--;

}

/* Otherwise I subscript moves backwards, ready to compare the next number. */

Else

i++;

}

/*

* At this time: i=j, array has not been processed for judgement

* and array[begin+1] ~ Array[i-1] are less than array[begin]

* and array[i+1] ~ Array[end] are greater than array[begin]

*

* Next: Compare and process with Array[begin] and Array[i]

* The purpose is to determine whether array[i] should be divided between the left and right groups,

* Also put the value of Array[begin] at the split line position.

*

* If the value of array[i] is greater than array[begin], place Array[begin] before I

* That is, array[begin] and I before the number of transposition

*/

if (Array[i] > Array[begin])

i--;


/* Place Array[begin] in position i points */

if (Swap (&array[begin], &array[i]) = = 1)

/* Show only data between Begin and end subscripts */

Show (array, maxlen, begin, end);


/* Then sort by Division line position I/*

Quicksort (array, maxlen, begin, I);

Quicksort (Array, MaxLen, J, end);

}

}

int main (int argc, char* argv[])

{

int array[10] = {49, 38, 65, 97, 48, 13, 27, 11, 56, 45};

int maxlen = sizeof (array)/sizeof (int);


Show (array, MaxLen, 0, maxlen-1); /* Print Initial order */

Quicksort (array, maxlen, 0, maxlen-1);

Show (array, MaxLen, 0, maxlen-1); /* Print Final results */

GetChar ();


return 0;

}


Using split-treatment method to realize quick-row

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.