3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
[C/C++ 演算法]-資料結構與演算法(文魁):交換排序   線上執行結果:http://www.tutorialspoint.com/compile_c_online.php     /* =============== Program Description =============== */ /* 程式名稱 : 6_3.cpp */ /* 程式目的 : 交換排序 */ /* 輸 入 : 排序前的整數陣列資料 */ /* 輸 出 : 排序後的整數陣列資料 */ /* =================================================== */ #define n 20 // 宣告標頭檔 #include "stdio.h" // 宣告函式原型 // 利用傳址的方式把Array A傳進exchange_sort函式中 void exchange_sort(int *A); // 利用傳參考的方式把Value1,Value2兩個變數,傳入swap函式中 void swap(int *Value1, int *Value2); void main(void) { int i; int A[n]={1,21,0,47,60, 15,84,65,77,88, 100,93,8,17,36, 5,24,63,72,20}; // 排序前的資料 printf(" Data before sorting \n"); for(i=0;i<n;i++) printf("%d",A[i]); printf("\n"); exchange_sort(A); // 排序後的資料 printf(" Data after sorting \n"); for(i=0;i<n;i++) printf("%d",A[i]); printf("\n"); getchar(); } void exchange_sort(int *A) { int i,j; for (i = 0 ; i <= n-2 ; i++) for (j = i+1 ; j <= n-1 ; j++) if ( A[i] > A[j] ) swap(&A[i],&A[j]); } void swap(int *Value1, int *Value2) { int temp; temp = *Value2; *Value2 = *Value1; *Value1 = temp; }  

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦