寫了2天的程序,終於算是解決了,歸併排序整了我一天多啊!強悍的思想。
程序還是需要敲代碼才能夠長進。
努力吧,雲天。未來不是夢。
總結一下:
1:程序要不斷的重複的敲,才能夠理解深入;
2:用良好的調試工具必不可少(強大的GDB);
3:程序+思考+草稿紙=成功。草稿紙也是王道啊,特別是下標不明確的時候,用草稿紙纔是王道。
4:堅持,堅持那份堅持,你就會邁着堅實的步伐走向成功。
5:飯,可七日不食;水,可三日不飲;然程序不可一日不寫!!!
說明:
幫助能過的同仁,共同進步。特將此程序發佈在網上。這些程序都是經過調試的。
調試的環境如下:
系 統:linux_redhat_5.5_i386
編譯器:gcc
時 間:2012.10.10——2012.10.12
程序附錄如下:
#include <stdio.h>
#include <stdlib.h>
#define N 154
void rand_arr(int *a)
{
int i = 0;
for (i = 0; i < N; i++) {
*(a+i) = rand()%100;
}
}
void pri(int *a)
{
int i = 0;
for (i = 0; i < N; i++) {
printf("the %5d number: %d\n", i+1, *(a+i));
}
printf("\n");
}
/*---------------------------insert sort-----------------------------*/
/* Function: insert sort minimum -> maximum
*
* Average time:n^2;
* The worst: n^2;
* ADDIT: O(1);
*
* Thought: traverse n-1 times;
* Method: insert the number to the ordered serial;
* and the ordered serial start the second array element;
*
* Parameter: a stand the array's physic address
*/
/* The first method */
void insert_direct_sort(int *a)
{
int i, j, key;
for (i = 1; i < N; i++) {
key = *(a+i);
j = i-1;
/* attention: if want minimum -> maximum ,cann't use *(a+j) > key as condition,
* because the random number is big enough;
* so if want minimum -> maximum must use this condition :
* while ((*(a+j) > key ) && (j >= 0));
*/
while (*(a+j) > key && (j >= 0)) {
*(a+j+1) = *(a+j);
j--;
}
*(a+j+1) = key;
}
}
/* The second method */
void insert_reduce_sort(int *a)
{
int i, j, m, s, key;
for (i = 1; i < N; i++) {
key = *(a+i);
s = 0;
j = i - 1;
while (s <= j) {
m = (s + j)/2;
if (*(a+m) > key) j = m - 1;
else s = m + 1;
}
for (j = i-1; j >= s; j--) {
*(a+j+1) = *(a+j);
}
*(a+s) = key;
}
}
/*---------------------------exchange sort-----------------------------*/
/* Function: exchange sort of quick sort
*
* Average time:n*log(n);
* The worst: n^2;
* ADDIT: O(log(n));
*
* Thought: divide and conquer
*
* Method: use the first number as the consult :
* if "right < insult" exist , swap to left; else contrast ;
* Parameter: a stand array;
* t stand the start subscript;
* w stand the end subscript;
*/
/* The first method */
void exchange_quick_sort_method1(int *a, int t, int w)
{
if (t >= w) return;
int i, j, key;
i = t;
j = w;
key = *(a + i);
while (i < j) {
while ((i < j) && (*(a+j) <= key)) j--;
if (i < j) {
*(a+i) = *(a+j);
i++;
}
while ((i < j) && (*(a+i) >= key)) i++;
if (i < j) {
*(a+j) = *(a+i);
j--;
}
}
*(a+j) = key;
exchange_quick_sort_method1(a, t, j-1);
exchange_quick_sort_method1(a, j+1, w);
}
/* The second method */
inline void exchange_quick_sort_method2_swap(int *a, int i, int j)
{
int tmp ;
tmp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = tmp;
}
void exchange_quick_sort_method2(int *a, int left, int right)
{
if (left >=right) return ;
int last, i;
int tmp = *(a+left);
last = left+1;
for (i = left +1; i <= right; i ++) {
if (*(a+i) < *(a+left)) {
exchange_quick_sort_method2_swap(a, last, i);
last++;
}
}
for (i = left+1; i < last; i++) {
*(a+i-1) = *(a+i);
}
*(a+i-1) = tmp;
exchange_quick_sort_method2(a, left, last-2);
exchange_quick_sort_method2(a, last, right);
}
/* Function: exchange sort of bubble sort
*
* Average time:n^2;
* The worst: n^2;
* ADDIT: O(1);
*
* Thought: traverse n-1 times; minimum -> maximum
*
* Method: use the first number as the consult :
* from the second element to the end of pell-mell array;
* if the element <= consult ,exchange;
* Parameter: a stand array;
*/
void exchange_bubble_sort(int *a)
{
int i, j, tmp;
for (i = 1; i < N; i++) {
for (j = 0 ; j < N - i; j++) {
if (*(a+j) < *(a+j+1)) {
tmp = *(a+j+1);
*(a+j+1) = *(a+j);
*(a+j) = tmp;
}
}
}
}
/*---------------------------select sort-----------------------------*/
/* Function: select sort of ordinary sort
*
* Average time:n^2;
* The worst: n^2;
* ADDIT: O(1);
*
* Thought: traverse n-1 times ,and select the minimum number
* and insert to the ordered array
*
* Method: select the minimum number from the pell-mell array
* Parameter: a stand array;
*/
void select_ordinary_sort(int *a)
{
int i, j, min;
for (i = 0; i < N-1; i++) {
min = i;
for ( j = i; j < N; j++) {
if (*(a+j) < *(a+min)) min = j;
}
if (i != min) {
j = *(a+i);
*(a+i) = *(a+min);
*(a+min) = j;
}
}
}
/* Function: select sort of heap sort
*
* Average time:n(log(n));
* The worst: n(log(n));
* ADDIT: O(1);
*
* Thought: first step: creat a minimum heap;
* second step: exchange the root and the end of current heap;
*
* Method:
* the first step: from the <n/2>th element to the first element; creat a heap;
* the second step: exchange the root element and the end of the current heap;
* and creat a new heap;
*
* Parameter: a stand array;
* s stand the start of heap;
* e stand the end of heap;
*/
void select_heap_sort_shift(int *a, int s, int e)
{
int key, i, j;
i = s;
j = i*2;
key = *(a+i);
while (j <= e) {
if ((j < e) && (*(a+j) > *(a+j+1))) j++;
if (*(a+j) < key) {
*(a+i) = *(a+j);
i = j;
j *=2;
}
else j = e + 1;
}
*(a+i) = key;
}
void select_heap_sort(int *a)
{
int i, key;
for (i = N/2; i >= 1; i--) {
select_heap_sort_shift(a, i, N);
}
for (i = N-1; i >=2; i--) {
key = *(a+1);
*(a+1) = *(a+i);
*(a+i) = key;
select_heap_sort_shift(a, 1, i-1);
}
key = *a;
for (i = 1; i < N; i++) {
if (*(a+i) > key) {
*(a+i-1) = *(a+i);
}
else break; //the else can not suspension, to judge the other hand
}
*(a+i-1) = key;
}
/*---------------------------merge sort-----------------------------*/
/* Function: merge sort
*
* Average time:n(log(n));
* The worst: n(log(n));
* ADDIT: O(n);
*
* Thought: merge two or more ordered array to one ordered array
*
* Method:
* the first step: see the N numbers a N ordered array
* the second step: merge the interfacing two ordered array,
* obotain <N/2> 2 element or 1 element ordered array
* the third step: merge the two interfaceing ......
* untile obtain one ordered array
* Parameter: a stand array;
* t stand tmp attay;
* s stand the length of compared array
* h stand start subscrip of the ordered array 1,
* m stand end subscrip of the ordered array 1,and one before of the array 2
* w stand end subscrip of the ordered array 2;
*/
/* merge two ordered array as one array */
void merge_sort_shift_join_2array(int *a, int h, int m, int w, int *t)
{
int i, j, k;
i = h;
j = m + 1;
k = h - 1;
while ((i <= m) && (j <= w)) {
k++;
if (*(a+i) <= *(a+j)) {
*(t+k) = *(a+i);
i++;
}
else {
*(t+k) = *(a+j);
j++;
}
}
if (i > m)
while(j <= w) {
k++;
*(t+k) = *(a+j);
j++;
}
else
while (i <= m) {
k++;
*(t+k) = *(a+i);
i++;
}
}
/* while each will ordered array length is s, ordering the array */
void merge_sort_shift_tag_ordered_each_array(int s, int *a, int *t)
{
int i = 0;
/* before N - 2*s +1 elements | print to thought */
while (i < N - 2*s + 1) {
merge_sort_shift_join_2array(a, i, i+s-1, i+2*s-1,t);
i = i +2*s;
}
/* betwen N - 2*s +1 and N -s + 1 ,
* which means the leave elements less than 2*s but greater than s
* | print to thought
*/
if (i < (N - s + 1)) merge_sort_shift_join_2array(a, i, i+s-1, N-1, t);
/* betwen N -s + 1 and N | print to thought */
else
while (i < N){
*(t+i) = *(a+i);
i++;
}
}
void merge_sort(int *a)
{
int t[N], s = 1, i;
while (s < N) {
merge_sort_shift_tag_ordered_each_array(s, a, t);
s *=2;
if (s < N) {
merge_sort_shift_tag_ordered_each_array(s, t, a);
s *=2;
}
else {
i = 0;
while (i < N) {
*(a+i) = *(t+i);
i++;
}
}
}
}
int main()
{
int a[N];
rand_arr(a);
pri(a);
// insert_direct_sort(a);
// insert_reduce_sort(a);
// exchange_quick_sort_method1(a, 0, N-1);
// exchange_quick_sort_method2(a, 0, N-1);
// exchange_bubble_sort(a);
// select_ordinary_sort(a);
// select_heap_sort(a);
merge_sort(a);
pri(a);
return 0;
}