C语言几种排序方法程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
for(i = 0; i<9; i++)
{
printf("%d\t",a[i]);
if((i+1) % 5 == 0)
printf("\n");
}
printf("\n");
return 0;
}
5.选择排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
temp = a[j];
flag = j;
}
}
a[flag] = a[i];
a[i] = temp;
}
for(i = 0; i<10; i++)
{
printf("%d\t", a[i]);
if((i+1)%5 == 0)
{
printf("\n");
}
}
//printf("\n");
return 0;
for(i = 0; i<size; i++)
{
for(j = 0; j < (size - i-1); j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag++;
}
}
if(flag == 0)
break;
flag = 0;
{
for(j = i+1; j<10; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(i = 0; i<10; i++)
{
printf("%d\t", a[i]);
if((i+1)%5 == 0)
printf("\n");
}
{
for(j=i+1; j<9; j++)
{
if(a[j] < a[i])
{
temp = a[j];
for(z = j-1; z >= i; z--)
{
a[z+1] = a[z];
}
a[i] = temp;
}
}
}
for(i = 0; i<9; i++)
{
printf("%d\t", a[i]);
if((i+1)%5 == 0)
int mid = 0;
mid = (begin+end)/2;
x = begin;
y = mid+1;
c = (int *)malloc((end+1)*4);
if(c == NULL)
return 0;
for(i = begin; i<(end+1); i++)
{
if(x != (mid+1) && y != (end+1))
printf("\n");
}
printf("\n");
return 0;
}
3.归并
#include <stdio.h>
#include <stdlib.h>
int merge(int *a, int begin, int end)
{
int *c = NULL;
int i = 0;
int x = 0, y = 0;
下面是几种排序方法的程序:
1.交换排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[10] = {1,4,3,0,7,5,2,8,9,6};
int i = 0, j = 0;
int temp = 0;
for(i = 0; i< 9; i++)
}
//printf("\n");
return 0;
}
2.插入排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[9] = {5,8,3,9,2,0,4,7,10};
int temp = 0, i = 0, j = 0, z=0;
for(i=0; i<9; i++)
merge(a, begin, end);
return 0;
}
int main(void)
{
int i = 0;
int a[10] = {1,5,2,7,4,9,10,3,0,6};
sort(a,0,9);
for(i = 0; i<10; i++)
{
printf("%d\t",a[i]);
if((i+1)%5 == 0)
{
int a[10] = {0,2,5,3,7,1,9,10,6,4};
int temp = 0, flag = 0;
inti = 0, j = 0;
for(i = 0; i<9; i++)
{
temp = a[i];
flag = i;
for(j = i+1; j < 10; j++)
{
if(temp > a[j])
{
if(a[x] < a[y])
{
c[i] = a[x];
x++;
}
else
{
c[i] = a[y];
y++;
}
}
else if(x != (mid+1))
{
c[i] = a[x];
x++;
}
else
{
c[i] = a[y];
y++;
}
}
for(i = begin; i<end+1; i++)
{
a[i] = c[i];
printfBiblioteka Baidu"\n");
}
return 0;
}
4.冒泡排序
#include <stdio.h>
#include <stdlib.h>
#define size 9
int main(void)
{
int a[size] = {3, 2, 6, 5, 4, 9, 8, 0, 7};
int i = 0, j = 0, flag = 0, temp = 0;
}
free(c);
return 0;
}
int sort(int *a, int begin, int end)
{
if(begin >= end)
return 0;
int mid = 0;
mid = (begin+end)/2;
sort(a, begin, mid);
sort(a, mid+1, end);
for(i = 0; i<9; i++)
{
printf("%d\t",a[i]);
if((i+1) % 5 == 0)
printf("\n");
}
printf("\n");
return 0;
}
5.选择排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
temp = a[j];
flag = j;
}
}
a[flag] = a[i];
a[i] = temp;
}
for(i = 0; i<10; i++)
{
printf("%d\t", a[i]);
if((i+1)%5 == 0)
{
printf("\n");
}
}
//printf("\n");
return 0;
for(i = 0; i<size; i++)
{
for(j = 0; j < (size - i-1); j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag++;
}
}
if(flag == 0)
break;
flag = 0;
{
for(j = i+1; j<10; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(i = 0; i<10; i++)
{
printf("%d\t", a[i]);
if((i+1)%5 == 0)
printf("\n");
}
{
for(j=i+1; j<9; j++)
{
if(a[j] < a[i])
{
temp = a[j];
for(z = j-1; z >= i; z--)
{
a[z+1] = a[z];
}
a[i] = temp;
}
}
}
for(i = 0; i<9; i++)
{
printf("%d\t", a[i]);
if((i+1)%5 == 0)
int mid = 0;
mid = (begin+end)/2;
x = begin;
y = mid+1;
c = (int *)malloc((end+1)*4);
if(c == NULL)
return 0;
for(i = begin; i<(end+1); i++)
{
if(x != (mid+1) && y != (end+1))
printf("\n");
}
printf("\n");
return 0;
}
3.归并
#include <stdio.h>
#include <stdlib.h>
int merge(int *a, int begin, int end)
{
int *c = NULL;
int i = 0;
int x = 0, y = 0;
下面是几种排序方法的程序:
1.交换排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[10] = {1,4,3,0,7,5,2,8,9,6};
int i = 0, j = 0;
int temp = 0;
for(i = 0; i< 9; i++)
}
//printf("\n");
return 0;
}
2.插入排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[9] = {5,8,3,9,2,0,4,7,10};
int temp = 0, i = 0, j = 0, z=0;
for(i=0; i<9; i++)
merge(a, begin, end);
return 0;
}
int main(void)
{
int i = 0;
int a[10] = {1,5,2,7,4,9,10,3,0,6};
sort(a,0,9);
for(i = 0; i<10; i++)
{
printf("%d\t",a[i]);
if((i+1)%5 == 0)
{
int a[10] = {0,2,5,3,7,1,9,10,6,4};
int temp = 0, flag = 0;
inti = 0, j = 0;
for(i = 0; i<9; i++)
{
temp = a[i];
flag = i;
for(j = i+1; j < 10; j++)
{
if(temp > a[j])
{
if(a[x] < a[y])
{
c[i] = a[x];
x++;
}
else
{
c[i] = a[y];
y++;
}
}
else if(x != (mid+1))
{
c[i] = a[x];
x++;
}
else
{
c[i] = a[y];
y++;
}
}
for(i = begin; i<end+1; i++)
{
a[i] = c[i];
printfBiblioteka Baidu"\n");
}
return 0;
}
4.冒泡排序
#include <stdio.h>
#include <stdlib.h>
#define size 9
int main(void)
{
int a[size] = {3, 2, 6, 5, 4, 9, 8, 0, 7};
int i = 0, j = 0, flag = 0, temp = 0;
}
free(c);
return 0;
}
int sort(int *a, int begin, int end)
{
if(begin >= end)
return 0;
int mid = 0;
mid = (begin+end)/2;
sort(a, begin, mid);
sort(a, mid+1, end);