Hello,everyone!See you again.Today i want to show you an important method----quicksort. It is a useful to sort,because it waste less time than many other sort ways.And for example,if I ask you to find the fifth biggest (ex.) number in an array,you can use it.Quicksort is based on Divide and Conquer,which is what i am learning.And i think two weeks later i will be good at it.Although the code of quicksort is simple,but i still use 30 minutes to pass it,because of while mistake and have not consider the array overflow,which should not happen.But i am being stronger and stronger.And one day i will be the toper.
The code may not be so simple,but it does work.I hope you can tell me how to be simpler,my friends.
Have fun coding,i_human!Have fun coding,everyone!
THE CODE:
#include "stdafx.h"
#include<iostream>
using namespace std;
void quicksort(int a[],int i,int j);
int main()
{
int a[]={3,1,5,2,4};
quicksort(a,0,4);
for(int i=0;i<5;i++)
cout<<a[i]<<endl;
system("pause");
return 0;
}
void quicksort(int a[],int i,int j)
{
int k=a[i];
int e=i;
int q=j;
int l;
if(i>=j)
return;
while(q>e)
{
while(a[q]>=k)
{
q--;
}
if(e<q)
{
a[e]=a[q];
}
else
{
l=e;
a[e]=k;
break;
}
while(a[e]<=k)
{
e++;
}
if(e<q)
{
a[q]=a[e];
}
else
{
l=q;
a[q]=k;
break;
}
}
quicksort(a,i,l-1);
quicksort(a,l+1,j);
}