ITfeng

 

2012年4月24日

排序----归并排序

摘要: 归并排序的前提是:归并前两个数组是有序的。归并排序的思路是先分成两半使用归并排序,然后比较大小,从小到大复制到一个零时数组中去;如果比较后,一方有剩余,那么将剩下的复制到临时数组,最后将排序好的数组拷贝回原数组。归并排序的时间复杂度是:nlogn#include<stdio.h>#include<stdlib.h>#define N 1000000int array[N];int temp[N];void init_array(int a[],int n);void print_array(int a[],int n);void guibing_sort(int a[] 阅读全文

posted @ 2012-04-24 16:32 ITfeng 阅读(306) 评论(0) 推荐(0) 编辑

排序----快速排序(方法2)

摘要: 这种快速排序的思路是:首先以a[start]为轴,不停得从数组的两端开始比较。从最右端开始,如果有比a[start]小的,那么赋值a[i];从最左端开始,如果有比a[start]大的,那么赋值a[j],以此往复,渐渐有序;注意的是递归跳出的条件,只有一个数就不需要排序,即start>=end#include<stdio.h>#include<stdlib.h>#define N 1000000int array[N];void init_array(int a[],int n);void print_array(int a[],int n);void quick_ 阅读全文

posted @ 2012-04-24 15:55 ITfeng 阅读(208) 评论(0) 推荐(0) 编辑

排序----快速排序(方法1)

摘要: 快速排序的思路是:首先拿a[start]作为轴,将原数组中比a[start]小的放small数组,将原数组中比a[start]大的放big数组,最后在将small数组 和a[start]值和big数组中的数复制回原数组。以此递归,使数组逐渐有序。快速排序的平均时间复杂度是nlogn。#include<stdio.h>#include<stdlib.h>#define N 1000000int array[N];int small[N];int big[N];void init_array(int a[],int n);void print_array(int a[],i 阅读全文

posted @ 2012-04-24 15:28 ITfeng 阅读(237) 评论(0) 推荐(0) 编辑

排序----插入排序

摘要: 插入排序的思路是:新插入的数与比它前面的数进行比较,如果新插入的数比它前面的数小,那么比它前面的数后移;否则,就找到了新插入的数的位置插入排序的时间复杂度是:O(n^2)#include<stdio.h>#include<stdlib.h>#define N 100int array[N];void init_array(int a[],int n);void print_array(int a[],int n);void insert_sort(int a[],int n);int main(){init_array(array,N);insert_sort(arra 阅读全文

posted @ 2012-04-24 14:46 ITfeng 阅读(274) 评论(0) 推荐(0) 编辑

导航