第二章 简单排序算法--插入排序,冒泡排序,选择排序
2012-11-14 12:43
302 查看
插入排序
/*
* Insertion_sort time complexity:
* Best-case: if the original sequence is sorted in a wanted-order: O(n)
* Worst-case: if the original array is sorted in a reverse order: O(n^2)
*/
void insertion_sort(int a[], int n)
{
int i, j, key;
for (j = 1; j < n; ++j) {
key = a[j];
i = j - 1;
while (i >= 0 && a[i] > key) {
a[i+1] = a[i];
--i;
}
a[i+1] = key;
}
}
冒泡排序
/*
* 算法复杂度 O(n^2)
*/
void BubbleSort(int a[], int n)
{
int i, j, temp;
//int flag = 0;
for (i = 0; i < n; ++i) {
//flag = 0;
for (j = n-1; j > i; --j) {
if (a[j] < a[j-1]) {
//flag = 1;
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
// if (flag == 0)
// break;
}
}
选择排序:
/*
* Selection_sort(A)
* First find the smallest element of A and exchang it with A[1],
* then find the second smallest element of A and exchang it with A[2]
* continue this manner for the first n-1 elements of A.
* Runnig time:
* O(n^2) both in the Best and Worst-case.
*/
int find_min(int a[], int start, int end)
{
int i;
int res = start;
for (i = start+1; i < end; ++i)
if (a[res] > a[i])
res = i;
return res;
}
void selection_sort(int a[], int n)
{
int j, k;
int temp;
for (j = 0; j < n-1; ++j) {
k = j;
k = find_min(a, j, n);
if (k != j) { // if a[j] is already the smallest we don't do this
temp = a[k];
a[k] = a[j];
a[j] = temp;
}
}
}
/*
* Insertion_sort time complexity:
* Best-case: if the original sequence is sorted in a wanted-order: O(n)
* Worst-case: if the original array is sorted in a reverse order: O(n^2)
*/
void insertion_sort(int a[], int n)
{
int i, j, key;
for (j = 1; j < n; ++j) {
key = a[j];
i = j - 1;
while (i >= 0 && a[i] > key) {
a[i+1] = a[i];
--i;
}
a[i+1] = key;
}
}
冒泡排序
/*
* 算法复杂度 O(n^2)
*/
void BubbleSort(int a[], int n)
{
int i, j, temp;
//int flag = 0;
for (i = 0; i < n; ++i) {
//flag = 0;
for (j = n-1; j > i; --j) {
if (a[j] < a[j-1]) {
//flag = 1;
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
// if (flag == 0)
// break;
}
}
选择排序:
/*
* Selection_sort(A)
* First find the smallest element of A and exchang it with A[1],
* then find the second smallest element of A and exchang it with A[2]
* continue this manner for the first n-1 elements of A.
* Runnig time:
* O(n^2) both in the Best and Worst-case.
*/
int find_min(int a[], int start, int end)
{
int i;
int res = start;
for (i = start+1; i < end; ++i)
if (a[res] > a[i])
res = i;
return res;
}
void selection_sort(int a[], int n)
{
int j, k;
int temp;
for (j = 0; j < n-1; ++j) {
k = j;
k = find_min(a, j, n);
if (k != j) { // if a[j] is already the smallest we don't do this
temp = a[k];
a[k] = a[j];
a[j] = temp;
}
}
}
相关文章推荐
- 简单排序算法:冒泡排序,选择排序,插入排序
- 简单排序算法(冒泡排序、选择排序、插入排序)
- (九)数据结构之简单排序算法实现:冒泡排序、插入排序和选择排序
- 选择排序、插入排序、冒泡排序python实现
- Java实现冒泡排序,选择排序,插入排序
- 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)
- 初级排序之冒泡排序,插入排序,选择排序比较
- 内排序(插入排序、冒泡排序、选择排序、shell排序、快速排序、归并排序、堆排序)
- Java基本排序(插入排序,冒泡排序,选择排序)
- 面试题:冒泡排序,选择排序,插入排序,快速排序,归并排序,堆排序
- 冒泡排序,选择排序,插入排序,快速排序的比较及优化
- 【算法】希尔排序,快速排序,选择排序,插入排序,冒泡排序
- c语言中冒泡排序、插入排序、选择排序算法比较
- 冒泡排序、选择排序、插入排序(java)
- 用JS实现冒泡排序、插入排序、选择排序、快速排序
- 冒泡排序,选择排序,插入排序
- 冒泡排序、选择排序、插入排序 算法实现(C++)
- 常用的简单排序之插入排序,冒泡排序,选择排序,希尔排序
- C语言排序算法集锦:选择排序,冒泡排序,插入排序,希尔排序,归并排序,堆排序,快排序
- 常用排序算法1--冒泡排序,选择排序,插入排序