[Fast sorting] Algorithm Implementation

Source: Internet
Author: User

Quick sorting is an application of grouping and recursion. The time complexity of the algorithm is O (nlogn ). The recursive depth of the algorithm is close to that of logn. Therefore, the required unit of work is O (logn ).

 

// Quick sorting
# Include <iostream. h>
Int count = 0;
Void swap (int & a, int & B)
{
Int temp;
Temp =;
A = B;
B = temp;
}
Int split (int array [], int low, int high)
{
Int I = low; // Save the position of the pivot element. The initial value is low.
Int j;
Int x = array [low];
 
For (j = low + 1; j <= high; j ++)
{
If (array [j] <= x)
{
I ++;
If (I! = J)
{
Swap (array [I], array [j]);
}
}
}
Swap (array [low], array [I]);
Return I;
}
Void print (int array [], int n)
{
For (int I = 0; I <8; I ++)
{
Cout <array [I] <"";
}
Cout <endl;
}
Void quick_sort (int array [], int low, int high)
{
Int k;
If (low {
K = split (array, low, high );
Quick_sort (array, low, k-1 );
Quick_sort (array, k + 1, high );
}
}
Void main ()
{
Int array [] = {5, 8, 4, 9, 3, 6, 7, 2 };
Quick_sort (array, 0, 7 );
Print (array, 7 );
}

 

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.