Quick sorting and Its Application

Source: Internet
Author: User

Quick Sort ):

The basic idea of Quick Sort is to split the records to be sorted into two separate parts by one Sort. the keywords of some records are smaller than those of other records, then the two parts of the records can be sorted separately to achieve the purpose of sorting the entire sequence.

Example: now we want to sort the Array {50, 10, 90, 30, 70, 40, 80, 60, 20. The algorithm is implemented as follows:


 

# Include <iostream> using namespace std; int Partion (int * array, int start, int end) {/* determine the validity of the parameter */if (array = NULL | start <0 | end <0 | start> end) {return-1 ;} /* If the pivot is the first element, array [start, end] is divided into two parts by pivot. The first part is smaller than the tkey, the latter part is greater than struct tkey */int struct tkey = array [start]; int I = start, j = end; while (I <j) {while (I <j & array [j]> = javastkey) {j --;} array [I] = array [j]; while (I <j & array [I] <= javastkey) {I ++;} array [j] = array [I];} array [I] = javastkey; return I;} void QuickSort (int * array, int start, int end) {/* determine the validity of the parameter */if (array = NULL | start <0 | end <0 | start> end) {return;} if (start <end) {int begin = Partion (array, start, end); QuickSort (array, start, begin-1); QuickSort (array, begin + 1, end );}} int main () {int array [] = {50, 10, 90, 30, 70, 40, 80, 60, 20}; QuickSort (array, 0, 8 ); for (int I = 0; I <8; I ++) {cout <array [I] <";}cout <endl ;} # include <iostream> using namespace std; int Partion (int * array, int start, int end) {/* determine the validity of the parameter */if (array = NULL | start <0 | end <0 | start> end) {return-1 ;} /* If the pivot is the first element, array [start, end] is divided into two parts by pivot. The first part is smaller than the tkey, the latter part is greater than struct tkey */int struct tkey = array [start]; int I = start, j = end; while (I <j) {while (I <j & array [j]> = javastkey) {j --;} array [I] = array [j]; while (I <j & array [I] <= javastkey) {I ++;} array [j] = array [I];} array [I] = javastkey; return I;} void QuickSort (int * array, int start, int end) {/* determine the validity of the parameter */if (array = NULL | start <0 | end <0 | start> end) {return;} if (start <end) {int begin = Partion (array, start, end); QuickSort (array, start, begin-1); QuickSort (array, begin + 1, end );}} int main () {int array [] = {50, 10, 90, 30, 70, 40, 80, 60, 20}; QuickSort (array, 0, 8 ); for (int I = 0; I <8; I ++) {cout <array [I] <";}cout <endl ;}

Next we will consider using the basic idea of fast sorting to solve the following problems.

Example 1: a number in the array appears more than half the length of the array. Find this number. For example, enter an array of 9 {1, 2, 3, 2, 2, 2, 5, 4, 2 }. Because number 2 appears five times in the array and exceeds half the length of the array, Output 2. (Sword refers to offer, interview question 29, number of pages: 163)

If a number in the array appears more than half the length of the array, the value in array [length/2] Must be this value after the array is sorted. Similarly, this value is the median of the entire array. That is, the number n/2 of the array whose length is n.

In consideration of the quick sorting algorithm, we first select a number in the array, and then adjust the order of the numbers in the array so that the numbers smaller than the selected number are on the left of the array, a number that is larger than the selected number is placed on the right of the selected number. If the subscript of the selected number is n/2, the number is the median of the array.

If its coordinate is greater than n/2, the median should be on its left, and we can search for it in the array on its left.

If its left side is less than n/2, the median should be on its right side, and we can look for it in the array on its right side.

Implement the above ideas:


 

# Include <iostream> using namespace std; bool gInputInvalid = false; int Partion (int * array, int start, int end) {if (array = NULL | start <0 | end <0 | start> end) {return-1;} int begin tkey = array [start]; int I = start, j = end; while (I <j) {while (I <j & array [j]> = foreign tkey) {j --;} array [I] = array [j]; while (I <j & array [I] <= foreign tkey) {I ++ ;} array [j] = array [I];} array [I] = repeated tkey; return I;}/* function name: GetMoreThanHalfNumber function: Get the number that appears more than half of the number in the array. If the number of input arrays exists more than half, we will not judge it here. Function parameter: int * array pointer int length return value: returns the number that appears more than half of the number in the array. */Int GetMoreThanHalfNumber (int * array, int length) {/* determine the validity of a parameter */if (array = NULL | length <0) {gInputInvalid = true; return 0;} int middle = length/2; int start = 0; int end = length-1; while (start <= end) {int index = Partion (array, start, end); if (index = middle) {break;} else if (index> middle) {end = index-1 ;}else {start = index + 1 ;}} return array [middle];} void Test (const char * testName, int * array, int length, int expectedMoreThanHalfNumber) {cout <testName <":"; if (GetMoreThanHalfNumber (array, length) = expectedMoreThanHalfNumber) {cout <"Passed. "<endl;} else {cout <" Failed. "<endl ;}int main () {int array1 [] = {1, 2, 3, 2, 2, 2, 5, 4, 2 }; test ("Test1", array1, sizeof (array1)/sizeof (int), 2); int array2 [] = {2, 2, 2, 2, 2, 1, 3, 4, 5}; Test ("Test2", array2, sizeof (array2)/sizeof (int), 2); int array3 [] = {1, 3, 4, 5, 2, 2, 2, 2, 2}; Test ("Test3", array3, sizeof (array3)/sizeof (int), 2 ); int array4 [] = {1}; Test ("Test4", array4, 1, 1) ;}# include <iostream> using namespace std; bool gInputInvalid = false; int Partion (int * array, int start, int end) {if (array = NULL | start <0 | end <0 | start> end) {return-1 ;} Int foreign tkey = array [start]; int I = start, j = end; while (I <j) {while (I <j & array [j]> = foreign tkey) {j --;} array [I] = array [j]; while (I <j & array [I] <= foreign tkey) {I ++ ;} array [j] = array [I];} array [I] = repeated tkey; return I;}/* function name: GetMoreThanHalfNumber function: obtain the number that appears more than half of the number in the array. If the number of input arrays exists more than half, we will not judge it here. Function parameter: int * array pointer int length return value: returns the number that appears more than half of the number in the array. */Int GetMoreThanHalfNumber (int * array, int length) {/* determine the validity of a parameter */if (array = NULL | length <0) {gInputInvalid = true; return 0;} int middle = length/2; int start = 0; int end = length-1; while (start <= end) {int index = Partion (array, start, end); if (index = middle) {break;} else if (index> middle) {end = index-1 ;}else {start = index + 1 ;}} return array [middle];} void Test (const char * testName, int * array, int length, int expectedMoreThanHalfNumber) {cout <testName <":"; if (GetMoreThanHalfNumber (array, length) = expectedMoreThanHalfNumber) {cout <"Passed. "<endl;} else {cout <" Failed. "<endl ;}int main () {int array1 [] = {1, 2, 3, 2, 2, 2, 5, 4, 2 }; test ("Test1", array1, sizeof (array1)/sizeof (int), 2); int array2 [] = {2, 2, 2, 2, 2, 1, 3, 4, 5}; Test ("Test2", array2, sizeof (array2)/sizeof (int), 2); int array3 [] = {1, 3, 4, 5, 2, 2, 2, 2, 2}; Test ("Test3", array3, sizeof (array3)/sizeof (int), 2 ); int array4 [] = {1}; Test ("Test4", array4, 1, 1 );}

 

Example 2: Enter n Integers to find the minimum k number. For example, if you enter the 8 numbers, the minimum 4 digits is. (Sword refers to offer, interview question 30, number of pages: 167)
This problem is solved based on the Partion function. If you adjust the number k Based on the array, so that all numbers smaller than the number k are located on the left of the array, all numbers larger than the k number are on the right of the array. After this adjustment, the k numbers on the left of the array are the minimum k numbers.

Similarly, we can replace n/2 in the previous question with k to solve the problem.


 

# Include <iostream> using namespace std; bool gInputInvalid = false; int Partion (int * array, int start, int end) {if (array = NULL | start <0 | end <0 | start> end) {return-1;} int begin tkey = array [start]; int I = start, j = end; while (I <j) {while (I <j & array [j]> = foreign tkey) {j --;} array [I] = array [j]; while (I <j & array [I] <= foreign tkey) {I ++ ;} array [j] = array [I];} array [I] = javastkey; return I;}/* function name: GetKLeastestNumbers function: get the minimum k numeric function parameters in the array: int * array pointer int length int k the minimum k number in the array */void GetKLeastestNumbers (int * array, int length, int k) {/* determine the validity of the parameter */if (array = NULL | length <0) {gInputInvalid = true; return;} int middle = k; int start = 0; int end = length-1; while (start <= end) {int index = Partion (array, start, end); if (index = middle) {break;} else if (index> middle) {end = index-1 ;}else {start = index + 1 ;}} void Test (const char * testName, int * array, int length, int k) {cout <testName <":" <endl; cout <"original array:"; for (int I = 0; I <length; I ++) {cout <array [I] <"" ;}cout <endl; GetKLeastestNumbers (array, length, k ); cout <"minimum k count:"; for (int I = 0; I <k; I ++) {cout <array [I] <"";} cout <endl;} int main () {int array1 [] = {4, 5, 1, 6, 2, 7, 3, 8}; Test ("Test1 ", array1, sizeof (array1)/sizeof (int), 4); int array2 [] = {4, 5, 1, 6, 2, 7, 3, 8 }; test ("Test2", array2, sizeof (array2)/sizeof (int), 8); int array3 [] = {4, 5, 1, 6, 2, 7, 3, 8}; Test ("Test3", array3, sizeof (array3)/sizeof (int), 1); return 0 ;}# include <iostream> using namespace std; bool gInputInvalid = false; int Partion (int * array, int start, int end) {if (array = NULL | start <0 | end <0 | start> end) {return-1;} int begin tkey = array [start]; int I = start, j = end; while (I <j) {while (I <j & array [j]> = foreign tkey) {j --;} array [I] = array [j]; while (I <j & array [I] <= foreign tkey) {I ++ ;} array [j] = array [I];} array [I] = javastkey; return I;}/* function name: GetKLeastestNumbers function: get the minimum k numeric function parameters in the array: int * array pointer int length int k the minimum k number in the array */void GetKLeastestNumbers (int * array, int length, int k) {/* determine the validity of the parameter */if (array = NULL | length <0) {gInputInvalid = true; return;} int middle = k; int start = 0; int end = length-1; while (start <= end) {int index = Partion (array, start, end); if (index = middle) {break;} else if (index> middle) {end = index-1 ;}else {start = index + 1 ;}} void Test (const char * testName, int * array, int length, int k) {cout <testName <":" <endl; cout <"original array:"; for (int I = 0; I <length; I ++) {cout <array [I] <"" ;}cout <endl; GetKLeastestNumbers (array, length, k ); cout <"minimum k count:"; for (int I = 0; I <k; I ++) {cout <array [I] <"";} cout <endl;} int main () {int array1 [] = {4, 5, 1, 6, 2, 7, 3, 8}; Test ("Test1 ", array1, sizeof (array1)/sizeof (int), 4); int array2 [] = {4, 5, 1, 6, 2, 7, 3, 8 }; test ("Test2", array2, sizeof (array2)/sizeof (int), 8); int array3 [] = {4, 5, 1, 6, 2, 7, 3, 8}; Test ("Test3", array3, sizeof (array3)/sizeof (int), 1); return 0 ;}


 

Related Article

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.