数组排序的六种方法

int array[] = {5, 1, 6, 4, 2, 9, 8, 3, 7};

int count = sizeof(array) / sizeof(array[0]);


1.      冒泡排序

原理:

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
  3. 针对所有的元素重复以上的步骤,除了最后一个。
  4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
                                   

算法简单实现:

int flag = 0;

for (int i = 0; i < count - 1 && flag== 0; i++) {

int flag = 1;

for (int j = 0; j < count - i - 1; j++) {

if (array[j] > array[j + 1]) {

int temp;

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

flag = 0;

}

}

}




2.      选择排序

  原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

算法实现:

for (int i = 0; i < count - 1; i++) {

int minIndex = i;

for (int j = minIndex + 1; j < count;j++) {

if (array[j] < array[minIndex]){

minIndex = j;

}

}

if (minIndex != i) {

int temp;

temp = array[minIndex];

array[minIndex] = array[i];

array[i] = temp;

}

}





3.     插入排序

原理:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

算法实现:

int array[] = {5, 1, 6, 4, 2, 9, 8, 3, 7};

int count = sizeof(array) / sizeof(array[0]);

for (int i = 0; i < count; i++) {

int j = i;

int temp = array[j];

while (j > 0 &&array[j - 1] > temp) {

array[j] = array[j - 1];

j--;

}

array[j] = temp;

}




4.      打乱排序

算法实现:

for (int i = 0; i < count; i++) {

int random = arc4random() % (i + 1);

int temp;

temp = array[i];

array[i] = array[random];

array[random] = temp;

}

for (int i = 0; i < count; i++) {

printf("[%2d] :%d \n", i, array[i]);

}





5.      桶排序

原理:

  1. 设置一个定量的数组当作空桶子。
  2. 寻访序列,并且把项目一个一个放到对应的桶子去。
  3. 对每个不是空的桶子进行排序。
  4. 从不是空的桶子里把项目再放回原来的序列中。

算法实现:

int array[] = {34, 56, 23, 78, 13, 57, 85, 56, 34, 34,

45};

int count = sizeof(array) / sizeof(*array);

int max = 0, min = 100;

for (int i = 0; i < count; i++) {

max = max > array[i] ? max :array[i];

min = min < array[i] ? min :array[i];

}

printf("max =%d, min = %d\n", max, min);

int *arr = malloc(sizeof(int) * (max -min));

for (int i = min; i <= max; i++) {

//判断

for (int j = 0; j < count; j++) {

if (array[j] == i) {

arr[i]++;

}

}

//输出

for (int j = 0; j < arr[i]; j++) {

printf("%d\n", i);

}

}

 



6.    选择排序

原理:利用递归算法(注意算法出口)首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

算法实现:

void quickSort(int *array, int count) {

if(count < 2) {

return;

}

int start = 0, end = count – 1;

int temp = array[start];

while (start < end)

{

while(start< end && array[end] > temp)

{

     end--;

}

if(start <end) {

        array[start]= array[end];

        start++;

}

while(start <end && array[start] < end) {

           start++:

}

If(start <end) {

           array[end] = array[start];

           end--;

}

}

     array[start] = temp;

     quickSort(array, start);

     quickSort(array + start + 1, count – start-  1);

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值