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

内部排序

时间:2016-05-08 13:20:32      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

内部排序 

一、插入      

  1.直接插入

  2.希尔排序

二、交换

  1.冒泡

  2.快速排序

三、选择

  1.简单选择

  2.堆排序

  3.归并排序

四、基数排序

本人能力有限,代码中可能存在问题,欢迎大家指正。我通过一些数据的测试,都基本通过,希望对一些朋友有用,谢谢

一、

/*************************************************************************
> File Name: insertSort.c
> Author: Z___001
> Mail:
> Created Time: Fri 06 May 2016 11:47:39 PM PDT
************************************************************************/

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

#define ElemType int
void print_array( ElemType *array, int len );

void directInsertSort( ElemType* array, int len )
{
int i = 0;
int j = 0;
ElemType valueTemp = 0;

for( i=1; i<len; i++ )
{
valueTemp = array[i];
for( j=i; j>0; j-- )
{
if( array[j-1] > valueTemp )
{
array[j] = array[j-1];
}
else
{
break;
}
}
if( j != i )
array[j] = valueTemp;

}

}

void shellGraph( ElemType* array, int len, int interval )
{
int i = 0;
int j = 0;
int k = 0;
ElemType valueTemp = 0;

for( i=0; i<interval; i++ )
{
for( j=i+interval;j<len; j += interval )
{
valueTemp = array[j];
for( k=j; k>=interval; k -= interval )
{
if( array[k-interval] > valueTemp )
{
array[k] = array[k-interval];
}
else
{
break;
}
}
if( k!=j )
array[k] = valueTemp;
}
}
}

void shellSort( ElemType* array, int len )
{
int d = 5;

for( ;d>0; d-- )
{
shellGraph( array, len, d );
print_array( array, len );

}

}

void print_array( ElemType *array, int len )
{
int i = 0;

for( i=0; i<len; i++ )
{
printf( "%d ",array[i] );
}

printf( "\n" );
}


int main( int argc, char* argv[] )
{

ElemType array[8] = {45,62,35,75,55,14,35,98};
ElemType arrayshell[10] = { 49,38, 65, 97, 76, 13, 27,48, 55,4 };

// directInsertSort( array, 8 );
// print_array( array, 8 );

print_array( arrayshell, 10 );
shellSort( arrayshell, 10 );
print_array( arrayshell, 10 );


return 0;
}

 

二、

/*************************************************************************
> File Name: selectSort.c
> Author: Z___001
> Mail: 
> Created Time: Sat 07 May 2016 01:11:00 AM PDT
************************************************************************/

#include<stdio.h>


void bubblingSort( int* array, int len )
{
int i = 0;
int j = 0;

for( i=0; i<len-1; i++ )
{
for( j=0; j<len-1-i; j++ )
{
if( array[j] > array[j+1] )
{
array[j] = array[j] + array[j+1];
array[j+1] = array[j] - array[j+1];
array[j] = array[j] - array[j+1];

}
}
}
}

void quickSort( int *array, int first, int end )
{
//int mid = 0;
int low = first;
int high = end;
int valueKey = 0;

valueKey = array[low];
if( low < high )
{
while( low < high )
{
while( array[high] > valueKey && low < high )
{
high--;
}
if( low<high )
{
array[low] = array[high];
low++;
}
while( array[low] < valueKey && low < high )
{
low++;
}
if( low<high )
{
array[high] = array[low];
high--;
}
}

array[low] = valueKey;
quickSort( array, first, low-1 );
quickSort( array, low+1, end );

}
}

void print_array( int* array, int len )
{
int i = 0;

for( i=0; i<len; i++ )
{
printf( "%d ",array[i] );
}
printf( "\n" );
}

int main( int argc, char* argv[] )
{
int array[12] = {9,8,7,6,54,32,7,8,5,40,3,40};

//bubblingSort( array, 12 );
print_array( array, 12 );

quickSort( array, 0, 11 );
print_array( array, 12 );

return 0;
}

三、

/*************************************************************************
> File Name: selectSort.c
> Author: Z___001
> Mail:
> Created Time: Sat 07 May 2016 07:19:20 AM PDT
************************************************************************/

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

void print_array( int *array, int len );

void simpleSelectSort( int* array, int len )
{
int i = 0;
int j = 0;

int iMin = 0;
int valueMin = 0;

for( i=0; i<len-1; i++ )
{
valueMin = array[i];
iMin = i;
for( j=i+1; j<len; j++ )
{
if( array[j] < valueMin )
{
valueMin = array[j];
iMin = j;
}
}
if( i != iMin )
{
array[i] = array[i] + array[iMin];
array[iMin] = array[i] - array[iMin];
array[i] = array[i] - array[iMin];
}
}
}

void justHeap( int* array, int len )
{
int i = 0;
int iNode = 0;
int lchild = 0;
int rchild = 0;
int valueTemp = 0;

iNode = ( len )/2 -1;
if( len <2 )
return;
if( len%2 == 0 )
{
if( array[iNode] < array[len-1] )
{
valueTemp = array[iNode];
array[iNode] = array[len-1];
array[len-1] = valueTemp;
}
}
for( i = iNode-1; i>=0; i-- )
{

lchild = 2*(i+1) -1;
rchild = lchild + 1;

if( array[i] < array[lchild] )
{
valueTemp = array[i];
array[i] = array[lchild];
array[lchild] = valueTemp;
}
if( array[i] < array[rchild] )
{
valueTemp = array[i];
array[i] = array[rchild];
array[rchild] = valueTemp;
}

}


}

void HeapSort( int* array, int len )
{
int i = 0;
int valueTemp = 0;

for( i=1; i<len; i++ )
{
justHeap( array, len-i+1 );
valueTemp = array[0];
array[0] = array[len-i];
array[len-i] = valueTemp;
}

}

void Merge( int* array, int low, int mid, int high, int *arrayTemp )
{
int i = 0;
int j = 0;
int k = low;
i = low;
j = mid+1;

while( i <= mid && j <= high )
{
if( array[i] < array[j] )
{
arrayTemp[k++] = array[i++];
}
else
{
arrayTemp[k++] = array[j++];
}
}
while( i<= mid )
{
arrayTemp[k++] = array[i++];
}
while( j<=high )
{
arrayTemp[k++] = array[j++];
}
for( i=low; i<=high; i++ )
{
array[i] = arrayTemp[i];
}

}

void MSort( int* array,int first, int end, int* arrayTemp )
{
int mid = 0;
if( first == end )
{
arrayTemp[first]=array[first];
}
else
{
mid = (first+end)/2;
MSort( array, first, mid,arrayTemp );
MSort( array, mid+1, end,arrayTemp );
Merge( array, first, mid, end, arrayTemp );
}
// print_array( arrayTemp, 10 );
}

void MergeSort( int* array, int len )
{
int *arrayTemp = NULL;
int i =0;

arrayTemp = ( int* )malloc( sizeof( int )*len );
MSort( array, 0, len-1, arrayTemp );

//for( i=0; i<len; i++ )
//{
// array[i] = arrayTemp[i];
// }

free( arrayTemp );
}

void print_array( int *array, int len )
{
int i = 0;

for( i=0; i<len; i++ )
{
printf( "%d ", array[i] );
}
printf( "\n" );
}

int main( int argc, char* argv[] )
{
int array[10] = { 5,89,48, 65, 12, 77, 14, 55, 38, 91};

//simpleSelectSort( array, 8 );
//HeapSort( array, 8 );
MergeSort( array,10 );
print_array( array, 10 );

return 0;
}

四、

/*************************************************************************
> File Name: BaseValueSort.c
> Author: Z___001
> Mail:
> Created Time: Sat 07 May 2016 09:30:16 AM PDT
************************************************************************/

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

void BaseSort( int* array, int len )
{
int **arrayBase = NULL;
int *iBase = NULL;
int i = 0;
int j = 0;
int k = 0;
int value = 0;
int baseValue = 1;
int flag = 1;
//int ch = -1;

arrayBase = ( int** )malloc( sizeof( int* )*10 );
for( i=0; i<10; i++ )
{
arrayBase[i] = (int *)malloc( sizeof( int )*len );
}
iBase = ( int* )malloc( sizeof(int)*10 );
for( i=0; i<10; i++ )
{
iBase[i] = 0;
}

while( flag )
{
flag = 0;
for( i=0; i<len; i++ )
{
value = (array[i]/baseValue)%10;
arrayBase[value][ iBase[value]++ ] = array[i];
//iBase[value] += 1;
if( value != 0 )
flag = 1;
}

k = 0;
for( i=0; i<10; i++ )
{
for( j=0; j<iBase[i]; j++ )
{
array[k++] = arrayBase[i][j];
}
iBase[i] = 0;
}
baseValue = baseValue*10;
}
}

int main( int argc, char* argv[] )
{

int i = 0;
int array[14] = { 12, 5, 8, 9, 23, 78, 123, 432, 456,7,9,1234,543,3456 };

BaseSort( array, 14 );

for( i=0; i<14; i++ )
{
printf("%d ", array[i]);
}
printf( "\n" );

return 0;
}

内部排序

标签:

原文地址:http://www.cnblogs.com/Z001/p/5470302.html

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