an important method -- quicksort

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);
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值