本文目錄一覽:
C語言快速排序的代碼
首先我贊成你直接要代碼的這種方法。
從你這個提問可以看出你對常用的排序算法都接觸過,並且都沒搞懂到底是怎麼回事。
現在的電子平台資源都很豐富了,硬件平台的運行速度可以做到很高了,在大多數的情況下可以考慮用空間換時間的方法,也就是說你應該先搞懂算法的本質,然後再自己去實現它,開始的時候可以不考慮時間上的損耗。
排序的本質就是兩個數比較大小,並根據其大小將其放到相應的位置。
記住其本質是什麼,你自己絕對可以使用相應的語言實現它。
用c語言編寫函數QuickSort()來實現快速排序
#include stdlib.h
#include stdio.h
#define MAXN 8
#define MOD 1024
void QuickSort(int *arr, int low, int high)
{
if (low = high) return;
//保存排序區間的 起始位置和終點位置
int left = low, right = high;
//默認 左邊第一個元素 為標誌
int key = arr[low];
while (low high)
{
while (low high arr[high] = key) –high;
arr[low] = arr[high];
while (low high arr[low] = key) ++low;
arr[high] = arr[low];
}
arr[low] = key;
//每次排序後都分成兩部分[left, low) (low, right]
//arr[low]的位置是一定是有序的
QuickSort(arr, left, low – 1);
QuickSort(arr, low + 1, right);
return;
}
int main(void)
{
int n;
scanf(“%d”, n);
int arr[MAXN] = {0};
int i;
for (i = 0; i n; ++i)
scanf(“%d”, arr[i]);
//輸入是默認為生活中習慣的數組左邊第一個為:編號1
int s, m;
scanf(“%d %d”, s, m);
//轉成計算機數組第一個為:編號0
s–; m–;
//快排
QuickSort(arr, s, m);
//輸出
for (i = s; i = m; ++i)
{
printf(“%d “, arr[i]);
}
return 0;
}
//測試數據
//8
//1 2 3 4 5 6 7 8
//2 6
輸出 6 5 4 3 2
用C語言編程實現快速排序算法
給個快速排序你參考參考
/********************** 快速排序 ****************************
基本思想:在待排序的n個記錄中任取一個記錄(通常取第一個記錄),
以該記錄為基準,將當前的無序區劃分為左右兩個較小的無
序子區,使左邊的記錄均小於基準值,右邊的記錄均大於或
等於基準值,基準值位於兩個無序區的中間位置(即該記錄
最終的排序位置)。之後,分別對兩個無序區進行上述的劃
分過程,直到無序區所有記錄都排序完畢。
*************************************************************/
/*************************************************************
函數名稱:static void swap(int *a, int *b)
參 數:int *a—整型指針
int *b—整型指針
功 能:交換兩個整數的位置
返 回 值:無
說 明:static關鍵字指明了該函數只能在本文件中使用
**************************************************************/
static void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int quickSortNum = 0; // 快速排序算法所需的趟數
/*************************************************************
函數名稱:static int partition(int a[], int low, int high)
參 數:int a[]—待排序的數據
int low—無序區的下限值
int high—無序區的上限值
功 能:完成一趟快速排序
返 回 值:基準值的最終排序位置
說 明:static關鍵字指明了該函數只能在本文件中使用
**************************************************************/
static int partition(int a[], int low, int high)
{
int privotKey = a[low]; //基準元素
while(low high)
{ //從表的兩端交替地向中間掃描
while(low high a[high] = privotKey) // 找到第一個小於privotKey的值
high–; //從high所指位置向前搜索,至多到low+1位置
swap(a[low], a[high]); // 將比基準元素小的交換到低端
while(low high a[low] = privotKey) // 找到第一個大於privotKey的值
low++; //從low所指位置向後搜索,至多到high-1位置
swap(a[low], a[high]); // 將比基準元素大的交換到高端
}
quickSortNum++; // 快速排序趟數加1
return low; // 返回基準值所在的位置
}
/*************************************************************
函數名稱:void QuickSort(int a[], int low, int high)
參 數:int a[]—待排序的數據
int low—無序區的下限值
int high—無序區的上限值
功 能:完成快速排序算法,並將排序完成的數據存放在數組a中
返 回 值:無
說 明:使用遞歸方式完成
**************************************************************/
void QuickSort(int a[], int low, int high)
{
if(low high)
{
int privotLoc = partition(a, low, high); // 將表一分為二
QuickSort(a, low, privotLoc-1); // 遞歸對低子表遞歸排序
QuickSort(a, privotLoc+1, high); // 遞歸對高子表遞歸排序
}
}
C語言快速排序代碼
採用快速排序,用遞歸實現
#include stdio.h
#define N 10 //定義排序數組元素個數
int Qsort(int start,int length,int a[])//start排序的起始,length是要排序序列長度
{
int x = a;
int i,j;
i = start;
j = length -1;
while(i j)
{
if(x a[j])
j–;
else if(x a[j])
{
a[i] = a[j];
a[j] = x;
i++;
}
else if(x a[i])
{
a[j] = a[i];
a[i] = x;
j–;
}
else
i++;
}
if(start length-1)
{
Qsort(start,i,a);
Qsort(i+1,length,a);
}
}
void main()
{
int a[N] = {0};
int i;
for(i = 0;i N;i++)
scanf(“%d”,a[i]);
Qsort(0,N,a);
for(i = 0;i N;i++)
printf(“%d “,a[i]);
}
程序執行時輸入N個數,對這N個數進行排序,可以預設N的長度
原創文章,作者:HMYV,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/136892.html