-
常用的排序算法
- #include <stdio.h>
- #include <stdlib.h>
- void heapify ( int a [] , int i , int size ){
- int left = i*2+1 ;
- int right = i*2 +2 ;
- int largest = i ;
- int temp = 0 ;
- if ( i <0 )
- return ;
- if ( left >=size )
- {
- heapify(a, i-1 , size);
- return 0;
- }
- else if ( a[left] > a[largest])
- {
- temp = a[largest] ;
- a[largest] = a[left] ;
- a[left] = temp ;
- }
- if (right >= size)
- ;
- else if ( a[right]> a[largest])
- {
- temp = a[largest] ;
- a[largest] = a[right] ;
- a[right] = temp ;
- }
- heapify(a,i-1,size);
- }
- void heap_sort( int a [] , int size ){
- int i ;
- for ( i = size-1 ; i>=0 ; i-- ){
- heapify(a,i,i+1) ;
- printf("size =%d max = %d
",i+1,a[0]);
- int temp = a [0] ;
- a[0] = a[i] ;
- a[i] = temp;
- }
-
- }
- void mergearray(int a[], int first, int mid, int last, int temp[])
-
- {
-
- int i = first, j = mid + 1;
- int m = mid, n = last;
- int k = 0;
- while (i <= m && j <= n)
- {
-
- if (a[i] < a[j])
- temp[k++] = a[i++];
- else
- temp[k++] = a[j++];
- }
-
- while (i <= m)
- temp[k++] = a[i++];
-
- while (j <= n)
- temp[k++] = a[j++];
-
- for (i = 0; i < k; i++)
- a[first + i] = temp[i];
-
- }
-
-
-
- void merge_sort( int a[] , int first , int last , int *temp){
- if ( first < last){
- int mid = (first + last) /2 ;
- merge_sort(a ,first,mid , temp) ;
- merge_sort(a,mid+1,last , temp) ;
-
- mergearray(a,first,mid,last , temp);
- }
- }
-
- int MergeSort( int a [] , int n ){
- int p [n+1];
-
- if ( p == NULL)
- return 0 ;
- else
- printf("开闭空间:%d
",n );
- merge_sort(a, 0,n-1,p) ;
-
-
- return 1 ;
- }
-
- selectionSort( int a[], long len ){
- int j = 0 , i = 0 ;
- int tmp ;
- long max_pos ;
- for ( i = len-1;i>=1;i--){
- max_pos = i ;
- for( j = 0 ; j<i ; j++ )
- if( a[max_pos] <a[j])
- max_pos = j ;
- if( max_pos !=i ) {
- tmp = a[i] ;
- a[i] = a[max_pos] ;
- a[max_pos] = tmp;
- }
- }
-
-
- }
-
- void doubleSort(int a [] , long len){
-
- int i = len -1 ;
- int j = len -1 ;
- int temp ;
- for ( i =len;i >0; i--){
- for ( j = i; j >0 ; j-- ){
- if( a[j]<a[j-1]){
- temp = a[j] ;
- a[j] = a[j-1] ;
- a[j-1] = temp ;
- }
- }
- }
- }
-
- int partiion( int a [], int left , int right){
- a[0] = a[left] ;
-
- while(left<right){
- while(left<right && a[0]<a[right])
- right -- ;
- if( left<right){
-
- a[left] = a[right];
- left ++ ;
- }
-
- while (left<right&&a[0]>a[left])
- left++;
- if( left < right ){
- a[right] = a[left];
- right -- ;
- }
- }
-
- a[left] = a[0] ;
- return left;
- }
-
- void quickSort( int a [], int left, int right){
- int i ;
- if ( left<right){
- i = partiion(a,left,right);
- quickSort(a,left,i-1);
- quickSort(a,i+1,right);
- }
- }
-
- void insertSort( int a[] , long len ){
- int i , j;
- int temp ;
- for ( i =0; i <=len -1 ; i++) {
- j = i+ 1;
- if ( a[j] <a[i])
- {
- temp = a[j] ;
- while(temp <a[i]){
- a[i+1] = a[i] ;
- i -- ;
- }
-
- a[i+1] = temp ;
- }
- i = j-1 ;
-
- }
- }
-
- void shellSort ( int a [] , int len ){
- int temp ,gap;
- int i , j ;
- for( gap = len/2; gap>0 ;gap/=2 ){
- for ( i =0 ; i <gap ; i ++ ){
- for ( j = i+gap ; j <len;j++){
-
- if(a[j]<a[j-gap]){
- temp = a[j] ;
- int k = j-gap ;
- while(k >=0 && a[k]>temp ){
- a [k+gap ] = a[k] ;
- k-= gap ;
- }
- a[k+gap ] = temp;
- }
- }
-
- }
- }
-
- }
-
- void swap(int a [] ){
- a[1] = a[2];
- }
- int main ( void ) {
-
- int a [6]={34,8,64,52,32,21};
- int b []={0,2,6,2,34,8,64,52,32,21,6};
-
-
- heap_sort(b,sizeof(b)/sizeof(b[0]));
- int i ;
-
-
-
- for( i =0; i<sizeof(b)/sizeof(b[0]) ;i++)
- printf("%d ",b[i]);
- printf("
");
- return 0;
- }
-
相关阅读:
安装 Office Online Server2016
HTML-冒泡算法
shell 中的$0 $1 $* $@ $# $$ $? $() $(())
线程池原理及C语言实现线程池
彻底搞懂Reactor模型和Proactor模型
TCP的三次握手与四次挥手理解及面试题
socket关闭的close和shutdown区别
C++ Virtual 完美诠释
Linux学习之CentOS--Linux系统的网络环境配置
Linux学习之CentOS--Linux网卡高级命令、IP别名及多网卡绑定
-
原文地址:https://www.cnblogs.com/php-rearch/p/6096695.html
Copyright © 2020-2023
润新知