C 常用排序演算法的實現

2021-06-24 11:56:50 字數 1065 閱讀 4133

最常用的演算法莫過於氣泡排序、選擇排序、插入排序、快速排序、歸併排序和希爾排序這幾種了。

標頭檔案如下:

#ifndef sorting_h_

#define sorting_h_

const static int sg_count = 10;

void bubblesort(int* pdata, int count);

void selectsort(int* pdata, int count);

void insertsort(int* pdata, int count);

void quicksort(int * pdata,int left,int right);

void shellsort(int * pdata, int count);

void mergesort(int * pdata, int start, int end);

#endif

實現檔案如下:

#include "sorting.h"

#include

using namespace std;

void swap(int &a,int &b) //交換

// 氣泡排序bubblesort()

void bublesort(int * num, int count)

}// 選擇排序selectsort()

void selectsort(int * num,int count)

pdata[ipos+1] = itemp;

}} // 快速排序quicksort()

void quicksort(int * pdata,int left,int right) }}

// 合併

void merge(int array, int start, int mid, int end)

}// 歸併排序,mergesort()

void mergesort(int * pdata, int start, int end)

}

常用排序演算法的C 實現

放假沒事幹,複習複習,有空再用python寫一遍 include include include include includeusing namespace std 氣泡排序 void bubblesort vector vec if sorted 選擇排序 void selectionsort ...

常用排序演算法的C 實現

放假沒事幹,複習複習,有空再用python寫一遍 include include include include includeusing namespace std 氣泡排序 void bubblesort vector vec if sorted 選擇排序 void selectionsort ...

常用排序演算法 C實現

1.1 氣泡排序 演算法描述 所給的n個數中,先拿第乙個數來和第二個比較,然後讓較大的乙個排在後面 即如果n1 n2,則讓n1與n2交換位置 然後又拿第二個數來和第三個數比較,又把較大的乙個排在後面,如此往下做下去,直到第n 1個數和第n個數比較完後,最大的那個數就會被公升到了最後面來.接下來又照同...