當前位置:首頁 » 編程語言 » 數據結構選擇排序演算法c語言代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

數據結構選擇排序演算法c語言代碼

發布時間: 2022-07-14 08:12:55

① C語言程序中的選擇法排序是

每次從待序記錄中選出排序碼最小的記錄,順序放在已排好的記錄序的後面,直到全部排完。
直接選擇排序
void selectSort(SortObject * pvector)
{
int i,j,k;
RecordNode temp;
for(i=0;i<pvector->n-1;i++)
{
k=i;
for(j=i+1;j<pvector->n-1;j++)
{
if(pvector->record[j].key>pvector->record[k].key)
k=j;
if(k!=i){
temp=pvector->record[k];
pvector->record[k]=pvector->record[i];
pvector->record[i]=temp;
}
}
}

② 求數據結構排序的程序(C語言版)

//------------插入排序---void InsertSort(SqList &L){//對順序表L作直接插入排序。 int i,j; for(i=2;i<=L.length;++i) if(LT(L.r[i].key,L.r[i-1].key))//「<」,需將L.r[i]插入有序子表 { L.r[0]=L.r[i];//復制為哨兵 L.r[i]=L.r[i-1]; for(j=i-2;LT(L.r[0].key,L.r[j].key);--j) L.r[j+1]=L.r[j];//記錄後移 L.r[j+1]=L.r[0];//插入到正確位置 }}//--------------冒泡排序---void BubbleSort(SqList &L){//L.r是待排序的文件,採用自下向上掃描,對L.r做冒泡排序 int i,j; int exchange; // 交換標志 for(i=1;i<L.length;i++) {// 最多做 n-1 趟排序 exchange=FALSE; // 本趟排序開始前,交換標志應為假 for(j=L.length-1;j>=i;j--) // 對當前無序區 R[i..n] 自下向上掃描 if(LT(L.r[j+1].key,L.r[j].key)) { // 交換記錄 L.r[0]=L.r[j+1]; //L.r[0]不是哨兵,僅做暫存單元 L.r[j+1]=L.r[j]; L.r[j]=L.r[0]; exchange=TRUE; // 發生了交換,故將交換標志置為真 } if(!exchange) // 本趟排序未發生交換,提前終止演算法 return; } }//-----------快速排序---int Partition(SqList &L,int low,int high){//交換順序表L中子表r[low..high]的記錄,樞軸記錄到位,並返回其所在位置,此時 //在它之前(後)的記錄均不大(小)於它。 KeyType pivotkey; L.r[0]=L.r[low];//用子表的第一個記錄作樞軸記錄 pivotkey=L.r[low].key;//樞軸記錄關鍵字 while(low<high) {//從表的兩端交替地向中間掃描 while (low<high&&L.r[high].key>=pivotkey) --high; L.r[low]=L.r[high];//將比樞軸記錄小的記錄移到低端 while (low<high&&L.r[low].key<=pivotkey) ++low; L.r[high]=L.r[low];//將比樞軸記錄大的記錄移到高端 } L.r[low]=L.r[0];//樞軸記錄到位 return low;//返回樞軸位置} void QSort(SqList &L,int low,int high){//對順序表L中的子序列L.r[low..high]進行快速排序 int pivotloc; if(low<high) {//長度大於1 pivotloc=Partition(L,low,high);//將L.r[low..high]一分為二 QSort(L,low,pivotloc-1);//對低子表遞歸排序pivotloc是樞軸位置 QSort(L,pivotloc+1,high);//對高子表遞歸排序 }} void QuickSort(SqList &L){//對順序表L作快速排序。 QSort(L,1,L.length);}//----------歸並排序---void Merge(RedType SR[],RedType TR[],int i,int m,int n){//將有序的SR[i..m]和SR[m+1..n]歸並為有序的TR[i..n] int j,k; for(j=m+1,k=i;i<=m&&j<=n;++k) {//將SR中記錄由小到大地並入TR if LQ(SR[i].key,SR[j].key) TR[k]=SR[i++]; else TR[k]=SR[j++]; } if(i<=m)//TR[k..n]=SR[i..m];將剩餘的SR[i..m]復制到TR while(k<=n&&i<=m) TR[k++]=SR[i++]; if(j<=n)//將剩餘的SR[j..n]復制到TR while(k<=n&&j<=n) TR[k++]=SR[j++];} void MSort(RedType SR[],RedType TR1[],int s,int t){//將SR[s..t]歸並排序為TR1[s..t]。 int m; RedType TR2[20]; if(s==t) TR1[t] = SR[s]; else { m=(s+t)/2;//將SR[s..t]平分為SR[s..m]和SR[m+1..t] MSort(SR,TR2,s,m);//遞歸地將SR[s..m]歸並為有序的TR2[s..m] MSort(SR,TR2,m+1,t);//將SR[m+1..t]歸並為有序的TR2[m+1..t] Merge(TR2,TR1,s,m,t);//將TR2[s..m]和TR2[m+1..t]歸並到TR1[s..t] }} void MergeSort(SqList &L){// 對順序表L作歸並排序。 MSort(L.r, L.r, 1, L.length);}//-----------堆排序---void HeapAdjust(SqList &H,int s,int m){//已知H.r[s..m]中記錄的關鍵字除H.r[s].key之外均滿足堆的定義, //本函數調整H.r[s]的關鍵字,使H.r[s..m]成為一個大頂堆 //(對其中記錄的關鍵字而言) int j; RedType rc; rc=H.r[s]; for(j=2*s;j<=m;j*=2) {//沿key較大的孩子結點向下篩選 if(j<m&&H.r[j].key<H.r[j+1].key) ++j;//j為key較大的記錄的下標 if(rc.key>=H.r[j].key) break;//rc應插入在位置s上 H.r[s]=H.r[j]; s=j; } H.r[s]=rc;//插入} void HeapSort(SqList &H){//對順序表H進行堆排序。 int i; RedType temp; for(i=H.length/2;i>0;--i)//把H.r[1..H.length]建成大頂堆 HeapAdjust(H,i,H.length); for(i=H.length;i>1;--i) { temp=H.r[i]; H.r[i]=H.r[1]; H.r[1]=temp;//將堆頂記錄和當前未經排序子序列Hr[1..i]中 //最後一個記錄相互交換 HeapAdjust(H,1,i-1);//將H.r[1..i-1]重新調整為大頂堆 }} void main(){ SqList S; printf("---------- 五種排序演算法 ----------\n"); InitList_Sq(S); printf(" 1.簡單插入排序\n"); InsertSort(S); Print_Sq(S); printf(" 2.冒泡排序\n"); BubbleSort(S); Print_Sq(S); printf(" 3.快速排序\n"); QuickSort(S); Print_Sq(S); printf(" 4.歸並排序\n"); MergeSort(S); Print_Sq(S); printf(" 5.堆排序\n"); HeapSort(S); Print_Sq(S);}

③ C語言——數據結構(排序-查找)

#include<stdio.h>
#include<stdlib.h>
#define N 10
void shellpass(int a[], int n, int d)
{
int i,j,temp;
for(i=d;i<n;i++)
{
if(a[i]<a[i-d])
{
temp=a[i];
for(j=i-d;j>=0&&temp<a[j];j-=d)
a[j+d]=a[j];
a[j+d]=temp;
}
}
}
void shellsort(int a[], int n, int delta[], int t)
{
int i, j;
for(i=0; i<t; i++)
{
shellpass(a, n, delta[i]);

printf("第%d趟希爾排序,增量為%d,排序之後的結果\n",i+1,delta[i]);
for(j=0; j<n; j++)
{
printf("%d\t",a[j]);
}
printf("\n");
}
}
void main()
{
int n=N, a[N];
int b[3] = {5,3,1};
int i;
printf("輸入%d個數\n", n);
for(i=0;i<n;i++)
{
printf("第%d個數:\t",i+1);
scanf("%d",&a[i]);
}

shellsort(a, n, b, 3);
printf("最終希爾排序之後的結果\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}

④ 排序演算法性能比較(數據結構)C語言程序

這題你只要把每個演算法的程序代碼看一下,在計算下就行
冒泡排序:兩個循環,從1加到N,(1+N)N/2
=
500500,最壞交換情況是每次判斷都要交換,既500500*3次
選擇排序:也是兩個循環,比較次數跟冒泡排序一樣500500,但是這個只要底層循環交換,既只需1000*3
=
3000次賦值。
插入排序:循環次數一樣500500,但是這個最壞情況是每比較一次就賦值一次,既需500500次賦值
希爾排序:時間復雜度是N^1.3倍,比較次數和賦值應該是1000^1.3次方。
歸並排序和快速排序,你去查查它的時間復雜度是怎麼算,O(lgN*N),好像有系數,演算法導論那本書上有,現在不記得是多少了。
希望能幫到你,

⑤ 數據結構C語言——實現各種排序演算法

剛做完的
#include <iostream>
using namespace std;

void BiInsertsort(int r[], int n) //插入排序(折半)
{
for(int i=2;i<=n;i++)
{
if (r[i]<r[i-1])
{
r[0] = r[i]; //設置哨兵
int low=1,high=i-1; //折半查找
while (low<=high)
{
int mid=(low+high)/2;
if (r[0]<r[mid]) high=mid-1;
else low = mid+1;
}
int j;
for (j=i-1;j>high;j--) r[j+1] = r[j]; //後移
r[j+1] = r[0];
}
}
for(int k=1;k<=n;k++) cout<<r[k]<<" ";
cout<<"\n";
}

void ShellSort ( int r[], int n) //希爾排序
{
for(int d=n/2;d>=1;d=d/2) //以d為增量進行直接插入排序
{
for (int i=d+1;i<=n;i++)
{
r[0] = r[i]; //暫存被插入記錄
int j;
for( j=i-d; j>0 && r[0]<r[j]; j=j-d) r[j+d] = r[j]; //記錄後移d個位置
r[j+d] = r[0];

}
}
for(int i=1;i<=n;i++) cout<<r[i]<<" ";
cout<<"\n";
}

void BubbleSort(int r[], int n) //起泡排序
{
int temp,exchange,bound;
exchange=n; //第一趟起泡排序的范圍是r[0]到r[n-1]
while (exchange) //僅當上一趟排序有記錄交換才進行本趟排序
{
bound=exchange;
exchange=0;
for (int j=1; j<bound; j++) //一趟起泡排序
if (r[j]>r[j+1])
{
temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
exchange=j; //記錄每一次發生記錄交換的位置
}
}
for(int i=1;i<=n;i++) cout<<r[i]<<" ";
cout<<"\n";
}

int Partition(int r[], int first, int end) //快速排序一次劃分
{
int i=first; //初始化
int j=end;
r[0]=r[first];
while (i<j)
{
while (i<j && r[0]<= r[j]) j--; //右側掃描
r[i]=r[j];
while (i<j && r[i]<= r[0]) i++; //左側掃描
r[j]=r[i];
}
r[i]=r[0];
return i; //i為軸值記錄的最終位置
}
void QuickSort(int r[], int first, int end) //快速排序
{
if (first<end)
{ //遞歸結束
int pivot=Partition(r, first, end); //一次劃分
QuickSort(r, first, pivot-1);//遞歸地對左側子序列進行快速排序
QuickSort(r, pivot+1, end); //遞歸地對右側子序列進行快速排序
}
}

void SelectSort(int r[ ], int n) //簡單選擇排序
{
int i,j,index,temp;
for (i=1; i<n; i++) //對n個記錄進行n-1趟簡單選擇排序
{
index=i;
for (j=i+1; j<=n; j++) //在無序區中選取最小記錄
if (r[j]<r[index]) index=j;
if (index!=i)
{
temp=r[i];
r[i]=r[index];
r[index]=temp;
}
}
for(i=1;i<=n;i++) cout<<r[i]<<" ";
cout<<"\n";
}

void main()
{
const int numv=12;
int a[3][numv]={{0,6,13,19,23,37,39,41,45,48,58,86},{0,86,58,48,45,41,39,37,23,19,13,6},{0,23,13,48,86,19,6,41,58,37,45,39}};
int z1[numv],z2[numv];
int m,n;
cout<<"請選擇測試數據類型:⑴正序 ⑵逆序 ⑶隨機 [ 若跳出,請按⑷ ]" <<endl;
cin>>m;
while(m>0&&m<4)
{
cout<<"請選擇排序演算法:⑴直接插入排序 ⑵希爾排序 ⑶冒泡排序 ⑷快速排序 \n ⑸簡單選擇排序"<<endl;
cin>>n;
switch(n)
{
case 1:
cout << "直接插入排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n直接插入排序結果為:" << "\n";
BiInsertsort(a[m-1],numv-1);
break;
case 2:
cout << "\n希爾排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n希爾排序結果為:" << "\n";
ShellSort(a[m-1], numv-1);
break;
case 3:
cout << "\n冒泡排序前:" << "\n";
for(int k=1;k<numv;k++) cout<<a[m-1][k]<<" ";
cout << "\n冒泡排序結果為:" << "\n";
BubbleSort(a[m-1], numv-1);
break;
case 4:
cout << "\n快速排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n快速排序結果為:" << "\n";
QuickSort(a[m-1],0,numv-1);
for(int i=1;i<numv;i++)
cout<<a[m-1][i]<<" ";
cout<<"\n";
break;
case 5:
cout << "\n簡單選擇排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n簡單選擇排序結果為:" << "\n";
SelectSort(a[m-1],numv-1);
break;

default:
cout<<"輸入錯誤!"<<endl;
}
m=0;
cout<<"請選擇測試數據類型:⑴正序 ⑵逆序 ⑶隨機 [ 若跳出,請按⑷ ]" <<endl;
cin>>m;
}
if(m==4) cout<<"(*^__^*) 再見!"<<endl;
else cout<<"輸入錯誤!"<<endl;
}

⑥ 求數據結構程序,用c++編寫的,{編寫一個程序實現直接選擇排序演算法{6,8,7,9,0,1,3,2,4,5}

int a[10] = {6,8,7,9,0,1,3,2,4,5};
int *b;
int count = 0;
b = calloc(4*10,1);
while(count < 10)
{
int c = INT_MAX;
for(int i=1;i<10;i++)
{
if(a[i] < c)
{
c = a[i];
}
if(c == count)break;
}
b[count] = c;
count++;
}

結果:b = {0,1,2,3,4,5,6,7,8,9}

⑦ 數據結構C++版 各種排序代碼

●插入排序(Insertion Sort)的基本思想是:每次將一個待排序的記錄,按其關鍵字大小插入到前面已經排好序的子文件中的適當位置,直到全部記錄插入完成為止。

//插入排序(一維數組)
function insert_sort($arr){
$count = count($arr);
for($i=1; $i<$count; $i++){
$tmp = $arr[$i];
$j = $i - 1;
while($arr[$j] > $tmp){
$arr[$j+1] = $arr[$j];
$arr[$j] = $tmp;
$j--;
}
}
return $arr;
}●選擇排序(Selection Sort)的基本思想是:每一趟從待排序的記錄中選出關鍵字最小的記錄,順序放在已排好序的子文件的最後,直到全部記錄排序完畢。

//選擇排序(一維數組)
function select_sort($arr){
$count = count($arr);
for($i=0; $i<$count; $i++){
$k = $i;
for($j=$i+1; $j<$count; $j++){
if ($arr[$k] > $arr[$j])
$k = $j;
if ($k != $i){
$tmp = $arr[$i];
$arr[$i] = $arr[$k];
$arr[$k] = $tmp;
}
}
}
return $arr;
}●冒泡排序的基本思想是:兩兩比較待排序記錄的關鍵字,發現兩個記錄的次序相反時即進行交換,直到沒有反序的記錄為止。

//冒泡排序(一維數組)
function bubble_sort($array){
$count = count($array);
if ($count <= 0) return false;
for($i=0; $i<$count; $i++){
for($j=$count-1; $j>$i; $j--){
if ($array[$j] < $array[$j-1]){
$tmp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $tmp;
}
}
}
return $array;
} ●快速排序實質上和冒泡排序一樣,都是屬於交換排序的一種應用。所以基本思想和上面的冒泡排序是一樣的。

//快速排序(一維數組)
function quick_sort($array){
if (count($array) <= 1) return $array;

$key = $array[0];
$left_arr = array();
$right_arr = array();
for ($i=1; $i<count($array); $i++){
if ($array[$i] <= $key)
$left_arr[] = $array[$i];
else
$right_arr[] = $array[$i];
}
$left_arr = quick_sort($left_arr);
$right_arr = quick_sort($right_arr);

return array_merge($left_arr, array($key), $right_arr);
}

⑧ C語言選擇排序法

這是選擇排序。先用a[0]與a[1]比較,當a[0]<a[1]時並不交換,而用k記下來現在a[0]最小……這樣一趟比較完後a[k]就是整個數組中最小的元素,把它與a[0]交換;第二趟,從a[1]開始重復前面的操作,那麼最後a[1]就是剩下的n-1個元素中最小的……看a[0]、a[1]已經由小到大排好了,當做完n-1趟時不就把整個數組都排好了嗎?注意:t=array[k];array[k]=array[i];array[i]=t;不是for(j=i+1;j<n;j++)的循環體,要等它循環完了後才執行一次。