排序

来源:互联网 发布:java指纹识别技术 编辑:程序博客网 时间:2024/05/01 19:56

#include <stdio.h>

void display_array(int *, int count);
void bubblesort(int*, int count);
void selectsort(int*, int count);
void insertsort(int*, int count);

void selection(int a[], int n);
void bubble(int a[], int n);
void insertion(int a[], int n);
void shellsort(int a[], int n);
void quick(int a[], int first, int end);
int quicksort(int a[], int i, int j);
void heapsort(int a[], int n);
void siftdown(int a[], int x, int n);
void heap(int a[], int n);

void main()
{
 int array[] = {1,3,2,43,4,46,78,9,98,26,61};
 printf("the array before sort:/n");
 display_array(array, 11);
 heapsort(array,11);
 heap(array, 11);
 display_array(array, 11);


}

void bubblesort(int* array, int count)
{
 for(int i = 0; i < count; i++)
  for (int j = count - 1; j > i; j--)
  {
   if (*(array + j) < *(array + j - 1))
   {
       int temp;
    temp = *(array + j);
    *(array + j) = *(array + j -1);
    *(array + j -1) = temp;
   }
  }
}

//
void selectsort(int* array, int count)
{
 for (int i = 1; i < count; i++)
 {
  for (int j = 0; j < i; j++)
  {
   if ( array[i] < array[j])
   {
    int temp = array[i];
    for (int x = i; x > j; x--)
    {
     array[x] =array[x - 1];
    }
    array[j] = temp;
   }
  }
 }
}

void insertsort(int* array, int count)
{
 int min_index = 0;

 for (int i = 0; i < count; i++)
 {
  for (int j = i; j < count; j++)
  {
   if (array[j] < array[min_index])
   {
    min_index = j;
   }
  }

  int temp;
  temp = array[min_index];
  array[min_index] = array[i];
  array[i] = temp;
 }
}

//显示数组
void display_array(int* array, int count)
{
 for (int i = 0; i <  count; i++)
 {
  printf("%d  ",*(array + i));
 }
 printf("/n");
}


//简单选择排序(验证正确,从小到大排序)
void selection(int a[], int n)
{
 for ( int i = 0; i < n; i++)
 {
  int index = i;
  for ( int j = i + 1; j < n; j++)
  {
   if ( a[j] < a[index] )
   {
    index = j;
   }
  }

  if ( index != i)
  {
   int temp;
   temp = a[index];
   a[index] = a[i];
   a[i] = temp;
  }  
 }
}

//简单插入排序(验证正确,从小到大排序)
void insertion(int a[], int n)
{
 for ( int i = 1; i < n; i++)
 {
  int t = a[i];

  for ( int j = i - 1; t < a[j] && j >= 0; j--)
  {
   a[ j + 1 ] = a[ j ];
  }

  a[ j + 1 ] = t;
 }
}

//冒泡排序(验证正确,从小到大排序),简单交换排序的特例
void bubble(int a[], int n)
{
 for ( int i = 0; i < n; i++)
 {
  for ( int j = n - 1; j > i; j-- )
  {
   if ( a[j] < a[ j - 1 ])
   {
    int temp;
    temp = a[j];
    a[j] = a[j-1];
    a[j-1] = temp;
   }
  }
 }
}

//希尔排序(验证正确,从小到大排序),插入排序的特例
void shellsort( int a[], int n)
{
 for ( int d = n/2; d >= 1; d = d/2)
 {
  for ( int i = d; i < n; i++)
  {
   int t = a[i];
   for ( int j = i - d; t < a[ j ] && j >= 0; j = j - d)
   {
    a[j + d] = a[ j ];
   }
   a[ j + d ] = t; 
  }
 }
}

//快速排序,一次排序过程
int quicksort( int a[], int i, int j)
{
 while ( i < j)
 {
  while ( a[i] < a[j] && j > i)
  {
   j--;
  }
  int temp;
  temp = a[j];
  a[j] = a[i];
  a[i] = temp;
  i++;

  while ( a[i] < a[j] && i < j)
  {
   i++;
  }
  temp = a[i];
  a[i] = a[j];
  a[j] = temp;
  j--;
 }
 return i;
}

//快速排序,交换排序的特例
void quick(int a[], int first, int end)
{
 int point;
 if ( first < end )
 {
  point = quicksort(a, first, end);
  quick(a, first, point-1);
  quick(a, point + 1, end);
 }
}

void heapsort(int a[], int n)
{
 for ( int i = n/2; i < n; i++)
 {
  siftdown(a, i, n);
 }
}

void siftdown(int a[], int x, int n)
{
 int i = x;
 int j = 2*i;
 while ( j < n)
 {
  if ( a[j] > a[j+1])
  {
   j++;
  }

  if (a[i] >a [j])
  {
   break;
  }
  else
  {
   int temp;
   temp = a[i];
   a[i] = a[j];
   a[j] = temp;
   i = j;
   j = 2 * i;
  }
 }
}

void heap(int a[], int n)
{
 for ( int i = 0; i < n; i++)
 {
  int temp;
  temp = a[i];
  a[i] = a[n-i];
  siftdown(a, i, n-i);
 }
}

原创粉丝点击