an important method -- quicksort
2016-09-14 23:13
295 查看
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);
}
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);
}
相关文章推荐
- vue.js视频教程,vue.js视频教程下载
- [Angular 2] More on *ngFor, @ContentChildren & QueryList<>
- Queue of Soldiers
- uestc 969 易位法字符串解密
- 18 UI美化transition 图片过渡
- 18 UI美化transition 图片过渡
- priority_queue的用法
- quick_sort新手入门
- 18 UI美化自定义形状shape
- 18 UI美化自定义形状shape
- 18 UI美化状态集合的位图selector
- 18 UI美化状态集合的位图selector
- BIOS/UEFI基础——块设备访问
- HDU 1005 Number Sequence 找循环区间的长度
- HDU 1159 Common Subsequence(LCS)
- Cannot convert value of type 'SecTrustResultType' to expected argument type 'UInt32'
- deque
- ERROR: x86 emulation currently requires hardware acceleration!的解决方法
- LibGdx文档译读(十二) InputEventQueue类
- handler消息机制源码解析