### 稀爾排序:
~~~
//希爾排序
#include <iostream>
#include <stdio.h>
void ShellSort( int* data, int left, int right){
int len = right-left +1;
int d =len;
while (d>1){
d =(d+1)/2;
for(int i =left; i<right+1-d;i++){
if(data[i+d]<data[i]){
int tmp = data[i+d];
data[i+d] = data[i];
data[i] =tmp;
}
}
}
}
void ShellSort2( int *data, int len){
int d =len;
while (d>1){
d =(d+1)/2;
for(int i =0; i<len-d;i++){
if(data[i+d]<data[i]){
int tmp = data[i+d];
data[i+d] = data[i];
data[i] =tmp;
}
}
for( int i=0; i<10;i++)
printf("%5d", data[i]);
printf("\n");
}
}
int main( )
{
int list[10];
int n=9;
int m=0;
printf(" Input 10 number: ");
for(int i=1; i<10;i++)
scanf("%d",&list[i]);
printf("\n");
ShellSort2(list,10);
//ShellSort(list,0,9);
printf("\n");
for(int i=0; i<10;i++)
printf("%5d",list[i]);
printf("\n");
}
~~~
### 冒泡排序:
~~~
//冒泡排序
#include <iostream>
#include <stdio.h>
void BubbleSort( int *list)
{
int temp;
for(int i=0;i<9;i++)
for(int j=0; j<9-i; j++){
if(list[j]>list[j+1])
{
temp =list[j];
list[j] =list[j+1];
list[j+1] =temp;
}
}
}
int main( )
{
int list [10];
int n=9,m=0;
printf(" Input 10 number: ");
for( int i=0 ; i<10;i++)
scanf("%d",&list[i]);
printf("\n");
BubbleSort(list);
for(int i =0;i<10;i++)
printf("%5d",list[i]);
printf("\n");
}
~~~
### 快速排序算法:
~~~
//快速排序算法
#include <iostream>
#include <stdio.h>
void improveqsort (int *list, int m, int n)
{
int k,t,i,j;
/*
for(int i =1; i<10;i++)
printf("%3d",list[i]);
*/
if(m<n)
{
i =m;
j=n+1;
k=list[m];
while(i<j)
{
for(i =i+1;i<n;i++)
if(list[i]>=k)
break;
for(j=j-1;j>m;j--)
if(list[j]<=k)
break;
if(i<j)
{
t =list[i];
list[i]=list[j];
list[j] =t;
}
}
t =list[m];
list[m]=list[j];
list[j]=t;
improveqsort(list,m,j-1);
improveqsort(list,i,n);
}
}
int main()
{
int list[10];
int n=9;
int m=0;
int i;
printf(" Input 10 number: ");
for( i=0;i<10;i++)
scanf("%d",&list[i]);
printf("\n");
improveqsort(list,m,n);
for(i=0;i<10;i++)
printf("%5d",list[i]);
printf("\n");
}
~~~
關于[程序算法藝術與實踐](http://blog.csdn.net/column/details/tac-programalgrithm.html)更多討論與交流,敬請關注本博客和新浪微博[songzi_tea](http://weibo.com/songzitea).
- 前言
- 螺旋矩陣、螺旋隊列算法
- 程序算法藝術與實踐:稀爾排序、冒泡排序和快速排序
- Josephu 問題:數組實現和鏈表實現
- 楊輝三角形算法
- 位圖排序
- 堆排序的實現
- Juggling算法
- 【編程珠璣】排序與位向量
- 取樣問題
- 變位詞實現
- 隨機順序的隨機整數
- 插入排序
- 二分搜索
- 產生不重復的隨機數
- 約瑟夫環解法
- 快速排序
- 旋轉交換或向量旋轉
- 塊變換(字符反轉)
- 如何優化程序打印出小于100000的素數
- 基本的排序算法原理與實現
- 利用馬爾可夫鏈生成隨機文本
- 字典樹,后綴樹
- B-和B+樹
- 程序算法藝術與實踐引導
- 程序算法藝術與實踐:基礎知識之有關算法的基本概念
- 程序算法藝術與實踐:經典排序算法之桶排序
- 程序算法藝術與實踐:基礎知識之函數的漸近的界
- 程序算法藝術與實踐:遞歸策略之矩陣乘法問題
- 程序算法藝術與實踐:遞歸策略之Fibonacci數列
- 程序算法藝術與實踐:遞歸策略基本的思想
- 程序算法藝術與實踐:經典排序算法之插入排序
- 程序算法藝術與實踐:遞歸策略之遞歸,循環與迭代