码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法(更新中)

时间:2015-07-01 18:45:21      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:include   排序算法   

#include<stdio.h>
#include<stdlib.h>

#define SUM 50

void bubble_sort(int arr[],int count);
void quick_sort(int arr[],int low,int high);
void select_sort(int a[],int count);
void swap_sort(int a[],int count);
void insert_sort(int a[],int count);
void shell_sort(int a[],int count);


int main(int argc,char**argv)
{
 int i = 0; 
 int arr[SUM];
 srand(time(NULL));
 for(i=0;i<SUM;i++)
 {
  arr[i] = rand()%100;
 }
 //bubble_sort(arr,SUM);
 //quick_sort(arr,0,SUM-1);
 //select_sort(arr,SUM);
 //swap_sort(arr,SUM);
 insert_sort(arr,SUM);
// shell_sort(arr,SUM);
 for(i = 0;i<SUM;i++)
 {
  printf("%d ",arr[i]);
 }
 printf("\n");
 return 0;
}


//***************************
//冒泡排序:
//双层循环,内层循环交换,外层循环一次则实现一个数据的冒泡排出
//***************************
void bubble_sort(int arr[],int count)
{
 int i,j;
 for(i=0;i<count;i++)
 {
  for(j=0;j<count - i - 1;j++)//for(j=0;j<count-1;j++)//多次重复循环
  {
   if(arr[j]>arr[j+1])//交换相邻
   {
    arr[j] = arr[j]+arr[j+1];
    arr[j+1] = arr[j] - arr[j+1];
    arr [j] = arr[j] - arr[j+1];
   }
  }
 }
 return; 
}


//***************************
//快速排序:
//采用二分思想,以某一数据为轴心,将数组分成两组,运用递归思想循环二分!
//***************************
void quick_sort(int a[],int low,int high)//快排,二分思想
{
 if(low>=high)
 {
  return;
 }
 int first = low;
 int last = high;
 int key = a[first];//将数组的第一个记录到key
 while(first < last)//循环完毕:将数组按二分思想分成两批
 {
  while(first<last && a[last] >= key)//从后往前查找第一个小于key的值
  {
   --last;
  }
  a[first] = a[last];//交换到数组首部
  while(first<last && a[first]<=key)//从前往后查找第一个比key大的值
  {
   ++first;
  }
  a[last] = a[first];//交换到数组尾部
 }
 a[first] = key;//将key保存到数组中间值所在的位置
 quick_sort(a,low,first-1);//将a[0]--a[first-1]再次排序
 quick_sort(a,first+1,high);//将a[first+1]--a[high]再次排序
 return;
}


//***************************
//选择排序:
//从后继数组中找到最小值(条件:与后继数组首位相比,
//记录下该首位位置及最小位置),放到该后继数组的首位,本例中就是a[i]
//***************************
void select_sort(int a[],int count)
{
 int i,j,k,temp;//i:循环条件;k:最小值下标;j:循环条件;temp:交换变量
 for(i=0;i<count;i++)
 {
  k = i;//
  for(j=i+1;j<count;j++)//寻找最小值
  {
   if(a[j]<a[k])
   {
    k = j;
   }
  }
  temp = a[k];//a[k]是最小值
  a[k] = a[i];//a[i]是后继数组的首位
  a[i] = temp;
 }
 return;
}



//***************************
//插入排序:在一个有序数列中插入一个数,使之仍然有序
//***************************
void insert_sort(int a[],int count)
{
 int i,j,temp;
 for(i=1;i<count;i++)
 {
  temp = a[i];
  for(j=i-1;j>=0;j--)
  {
   if(temp<a[j])
   {
    a[j+1] = a[j];
   }else
    break;
  }
  a[j+1] = temp;
 }
 return;
}


//***************************
//shell排序(希尔排序):由希尔提出的,
//一般初次取序列的一半为增量。
//去掉外层while循环,并将增量gap置为1,则是普通的插入排序
//***************************
void shell_sort(int a[],int count)
{
 int gap,i,j,temp;//增量gap
 gap=count/2;
 while(gap>0)
 {
  //***********************
  for(i=gap;i<count;i++)
  {
   temp = a[i];
   for(j=i-gap;j>=0;j -= gap)
   {
    if(temp<a[j])
    {
     a[j+gap] = a[j];
    }else
     break;
   }
   a[j+gap] = temp;
  }
  //gap == 1时,该段代码为插入排序
  //***********************
 gap = gap/2;
 }
 return;
}



//***************************
//交换排序:
//***************************
void swap_sort(int a[],int count)
{
 int i,j,temp;
 for(i = 0;i<count-1;i++)
 {
  for(j=i+1;j<count;j++)
  {
   if(a[j]<a[i])
   {
    temp = a[j];
    a[j] = a[i];
    a[i] = temp;
   }
  }
 }
}


本文出自 “咙叮咚员外” 博客,请务必保留此出处http://lddyw.blog.51cto.com/4151746/1669735

排序算法(更新中)

标签:include   排序算法   

原文地址:http://lddyw.blog.51cto.com/4151746/1669735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!