标签:
// sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void print ( int a[] ,int n )
{
for ( int i=0;i<n;i++) cout<<a[i]<<",";
cout<<endl;
}
// ?°?Y??Dò
void bubble_sort( int a[] , int n )
{
for ( int i=0 ; i < n; i++ ) {
for ( int j=i+1; j< n; j++ ) {
if ( a[i] > a[j] ) {
int tmp = a[i];
a[i] = a[j] ;
a[j] = tmp;
}
}
}
print ( a, 10 );
}
// 2?è???Dò
void insert_sort ( int a[] , int n )
{
int b[10] ={0,0,0,0,0,0,0,0,0};
for ( int i = 0; i < n ; i++ ) {
if ( a[i] < b[0] ) {
b[0] = a[i];
for ( int j=1 ; j<i-1; j++ ) b[j] = b[j+1];
}
if( a[i] > b[i-1] ) b[i] = a[i] ;
for ( int j = 0 ; j < i-1 ; j++ ) {
if ( a[i] > b[j] && a[i] < b[j+1] ) {
b[j] = a[i];
for ( int n=j+1; n<j ; n++) b[n] = b[n+1];
break;
}
}
}
print ( b, 10 );
}
// ??????Dò
void select_sort ( int a[] , int len )
{
for ( int i = 0 ; i < len ; i++ ) {
for ( int j=i+1; j< len; j++ ) {
if ( a[j] > a[i] ) {
int tmp = a[j] ;
a[j] = a[i];
a[i] = tmp;
}
}
}
print ( a, 10 );
}
// μY1é·?×é??Dò
void recursive_group_sort ( int a[] , int left , int right )
{
if ( left == right ) return;
int mid = (right+left)/2;
for( int i=left; i<= mid; i++) {
for( int j=mid+1; j <= right ; j++ ) {
if( a[i] > a[j] ) {
int tmp = a[j] ;
a[j] = a[i];
a[i] = tmp;
}
}
}
recursive_group_sort ( a, left , mid ) ;
recursive_group_sort ( a, mid+1 , right) ;
}
int main(int argc, char* argv[])
{
int a[10] = {5,2,8,9,3,7,6,0,1,4};
bubble_sort(a,10);
insert_sort(a,10);
select_sort(a,10);
cout<<"recursive group sort "<<endl;
recursive_group_sort( a, 0 , 9 );
print(a,10);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/fysola/p/4862562.html