快速排序
来源:互联网 发布:linux vim 查找字符串 编辑:程序博客网 时间:2024/05/16 06:28
//快速排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[10];
void search(int m, int n){
int i;
int temp1, temp2, temp3;
temp1 = m;
temp2 = n - 1;
if(m >= n){
return ;
}
while(1){
for(i = temp2; i > temp1; i--){
if(a[temp1] > a[i]){
temp3 = a[temp1];
a[temp1] = a[i];
a[i] = temp3;
//a[temp1] ^= a[i] ^= a[temp1] ^= a[i];
break;
}
}
temp2 = i;
for(i = temp1; i < temp2; i++){
if(a[temp2] < a[i]){
temp3 = a[temp2];
a[temp2] = a[i];
a[i] = temp3;
//a[temp2] ^= a[i] ^= a[temp2] ^= a[i];
}
}
temp1 = i;
if(temp1 == temp2){
break;
}
}
search(m, temp1);
search(temp2 + 1, n);
}
int main()
{
int i;
int N;
printf("Enter the quality:\n");
scanf("%d", &N);
printf("Enter the number: ");
for(i = 0; i < N; i++){
scanf("%d", &a[i]);
}
search(0, N);
for(i = 0; i < N; i++){
printf("%-4d", a[i]);
}
printf("\n");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[10];
void search(int m, int n){
int i;
int temp1, temp2, temp3;
temp1 = m;
temp2 = n - 1;
if(m >= n){
return ;
}
while(1){
for(i = temp2; i > temp1; i--){
if(a[temp1] > a[i]){
temp3 = a[temp1];
a[temp1] = a[i];
a[i] = temp3;
//a[temp1] ^= a[i] ^= a[temp1] ^= a[i];
break;
}
}
temp2 = i;
for(i = temp1; i < temp2; i++){
if(a[temp2] < a[i]){
temp3 = a[temp2];
a[temp2] = a[i];
a[i] = temp3;
//a[temp2] ^= a[i] ^= a[temp2] ^= a[i];
}
}
temp1 = i;
if(temp1 == temp2){
break;
}
}
search(m, temp1);
search(temp2 + 1, n);
}
int main()
{
int i;
int N;
printf("Enter the quality:\n");
scanf("%d", &N);
printf("Enter the number: ");
for(i = 0; i < N; i++){
scanf("%d", &a[i]);
}
search(0, N);
for(i = 0; i < N; i++){
printf("%-4d", a[i]);
}
printf("\n");
return 0;
}
1 0