C语言数组排序问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言数组排序问题相关的知识,希望对你有一定的参考价值。

入一组整数,对前半部分的数从大到小排序,对后半部分的数从小到大排序。
例如:
输入的是:1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11,
则按指定规则排序后的结果是:10 9 8 7 6 5 4 3 2 1 11 12 13 14 15 16 17 18 19 20

若输入的是:1 2 3 4 5 4 3 2 1,则按指定规则排序后的结果是:4 3 2 1 5 1 2 3 4

搞定, 用户手动输入数列,可以是正负数。宏定义 MAX_STRING_SIZE 决定用户输入的最大字符长度, MAX_ARRAY_SIZE决定用户最多输入的整数个数。

bubble_sort() 函数实现冒泡法排序, 第一个参数为被排序数组,第二个参数为数组长度,地三个参数为排序方式, 0 表示从小到大,非0表示从大到小。

/* vim: set et sw=4: */
#include <stdio.h>
#include <string.h>

#define MAX_STRING_SIZE (4096)
#define MAX_ARRAY_SIZE (100)

#define COMPARE(a, b, comp_type) (comp_type == 0 ? (a > b) : (a < b))

/* sort_type = 0, small to large, else in reverse order */
void bubble_sort(int * ar, int size, int sort_type)

int swapped, i, val;
do

swapped = 0;
for(i = 0; i < size - 1; i++)

if(COMPARE(ar[i], ar[i+1], sort_type))

val = ar[i];
ar[i] = ar[i+1];
ar[i+1] = val;
swapped++;


size--;
while(swapped != 0);


int main()

char buffer[MAX_STRING_SIZE];
char * p;
int array[MAX_ARRAY_SIZE];
int i, len, num_count;
char ch;

printf("Input number serials, seperated by white spaces!\\n");
len = num_count = 0;
while(((ch = getchar()) != \'\\n\') && len < MAX_STRING_SIZE)

if(ch >= \'0\' && ch <= \'9\')

buffer[len++] = ch;

else

switch(ch)

case \' \':
buffer[len++] = \'\\0\';
break;
case \'-\':
case \'+\':
buffer[len++] = ch;
break;
default:
printf("\\nInvalid character!\\n");
return -1;




buffer[len] = \'\\0\';

for(i = 0; i < len; i += (strlen(p)+1))

p = &buffer[i];
if(*p != \'\\0\')

if(num_count >= MAX_ARRAY_SIZE)

printf("array is full (total %d elements), remaining numbers are discarded!\\n", num_count);
break;

array[num_count++] = atoi(p);



if(num_count > 1)

/* sort first half by reverse order */
bubble_sort(array, num_count/2, 1);
/* sort last part */
if(num_count % 2)

bubble_sort(&array[num_count/2 + 1], num_count/2, 0);

else

bubble_sort(&array[num_count/2], num_count/2, 0);



printf("After sorting:\\n");
for(i = 0; i < num_count; i++)

printf("%d ", array[i]);

printf("\\n");

return 0;
参考技术A 正在学java,看到你的问题就用java写了一次,学过c的应该能看得懂吧
public class win
public static void main(String[] args )
int a[]=1,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10, 20 ,19, 18 ,17 ,16, 15 ,14 ,13, 12 ,11;
//int a[]=1 ,2, 3 ,4 ,5 ,4 ,3 ,2 ,1;
int len=a.length;
int temp;
System.out.print("排列前:");//打印排列前数据
for(int m=0;m<len;m++)
System.out.print(a[m]);
System.out.print(" ,");

for (int i=0;i<len;i++)
for (int j=1;j<len;j++)
if (j<len/2)
if(a[j]>a[j-1])
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;

else
if(a[j]<a[j-1])
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;





System.out.print("\n排列后:");//打印排列后数据
for (int k=0;k<len;k++)
System.out.print(a[k]);
System.out.print(" ,");



参考技术B 关键是长度问题,如果长度未知的话,输入时最好当做字符串,然后用strlen计算长度,然后用冒泡法,通过比较ASCii码 实现从大到小和从小到大的排序,这个人和c书上都有的。 参考技术C 关注中 参考技术D 一组(小到大)排序数字和一组(小到大)排序数字归并一组的话,首先以一组为基础,另一组的第一个数字和基础排序的组的中间数字比较,大的话向右移动,小的话相左移动,每次都是和中间的比较,
例如1组(1,3,5,6,7,8,9)2组(2,5,10,23,24,69)先把2组2和1组5比较,小的话向左移动,继续和1组3比较,小的话继续最后找到位置追加,2组5同上,最后都追加完成了,从后面输出就可以了!

利用Java语言,实现数组的排序

先输入十个数,将这十个数组成数组并以将序排列输出,然后再向这个数组插入一个数让这个数组依然按照降序排列输出,(要求向数组插入数时必须使用插入算法,不能使用排序算法)
实现效果如下:
请输入十个数:
12 34 2 4 5 6 7 9 31 21
降序排列后输出结果:
2 4 5 6 7 9 12 21 31 34
插入一个数:
23
插入一个数后的输出结果:
2 4 5 6 7 9 12 21 23 31 34

import java.util.Scanner;

public class Test
public static void main(String[] args)
Scanner scanner = new Scanner(System.in);
System.out.println("请输入十个数:");
int arr[] = new int[11];
for (int i=0;i<10;i++)
arr[i] = scanner.nextInt();

for (int i=0;i<10;i++)
for (int j=i;j<10;j++)
if (arr[i]<arr[j])
int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;



System.out.println("降序排列后输出结果:");
for (int i=0;i<10;i++)
System.out.print(arr[i] + " ");

System.out.println();
System.out.println("插入一个数:");
int ex = scanner.nextInt();
for (int i=0;i<10;i++)
if (ex>arr[i])
for (int j=10;j>i;j--)
arr[j] = arr[j-1];

arr[i] = ex;
break;


System.out.println("插入一个数后的输出结果:");
for (int i=0;i<11;i++)
System.out.print(arr[i] + " ");


参考技术A int[] i = new int[10];
int[] j = i.sort();
参考技术B // 快速排序算法
public int partition(int num[], int low, int high)
int m = num[low];// 记录关键字
while (low < high)
while (low < high && num[high] <= m)
--high;// 将比记录小的记录移到低端
num[low] = num[high];
while (low < high && num[low] >= m)
++low;// 将比记录大的记录移到高端
num[high] = num[low];

num[low] = m;// 返回记录轴的位置
return low;

//快速排序递归 的实现
public void QSort(int num[], int low, int high)
if (low < high) // 长度大于一
int pivotloc = partition(num, low, high);// 将数据一分为二
QSort(num, low, pivotloc - 1);// 递归排序
QSort(num, pivotloc + 1, high);

参考技术C 这个随便找本书,上面都有的呀

以上是关于C语言数组排序问题的主要内容,如果未能解决你的问题,请参考以下文章

C语言结构体数组排序

C语言 随机数组排序

C语言一维数组排序

C语言 关于数组中的数从小到大排序的问题

c语言指针数组排序?

c语言如何实现-数组排序,二分查找