C語言的冒泡排序和快速排序算法使用實例
冒泡排序法
題目描述:
用一維數(shù)組存儲學(xué)號和成績,然后,按成績排序輸出。
輸入:
輸入第一行包括一個整數(shù)N(1<=N<=100),代表學(xué)生的個數(shù)。
接下來的N行每行包括兩個整數(shù)p和q,分別代表每個學(xué)生的學(xué)號和成績。
輸出:
按照學(xué)生的成績從小到大進行排序,并將排序后的學(xué)生信息打印出來。
如果學(xué)生的成績相同,則按照學(xué)號的大小進行從小到大排序。
樣例輸入:
3
1 90
2 87
3 92
樣例輸出:
2 87
1 90
3 92
代碼:
#include <stdio.h> #include <stdlib.h> struct student { int number; int score; }; int main() { struct student students[101]; int n, i, j; struct student temp; while(scanf("%d",&n) != EOF) { //接收數(shù)據(jù) for(i = 0; i < n; i++) { scanf("%d%d",&students[i].number,&students[i].score); } //冒泡排序 for(i = 0; i < n - 1; i ++) { for(j = 0; j < n - i - 1; j ++) { if(students[j].score > students[j + 1].score) { temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; }else if(students[j].score == students[j + 1].score) { if(students[j].number > students[j + 1].number) { temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } } //輸出排序結(jié)果 for(i = 0; i < n; i ++) { printf("%d %d\n",students[i].number,students[i].score); } } return 0; }
快速排序法
題目描述:
有N個學(xué)生的數(shù)據(jù),將學(xué)生數(shù)據(jù)按成績高低排序,如果成績相同則按姓名字符的字母序排序,如果姓名的字母序也相同則按照學(xué)生的年齡排序,并輸出N個學(xué)生排序后的信息。
輸入:
測試數(shù)據(jù)有多組,每組輸入第一行有一個整數(shù)N(N<=1000),接下來的N行包括N個學(xué)生的數(shù)據(jù)。
每個學(xué)生的數(shù)據(jù)包括姓名(長度不超過100的字符串)、年齡(整形數(shù))、成績(小于等于100的正數(shù))。
輸出:
將學(xué)生信息按成績進行排序,成績相同的則按姓名的字母序進行排序。
然后輸出學(xué)生信息,按照如下格式:
姓名 年齡 成績
樣例輸入:
3
abc 20 99
bcd 19 97
bed 20 97
樣例輸出:
bcd 19 97
bed 20 97
abc 20 99
代碼
#include <stdio.h> #include <stdlib.h> #include <string.h> struct student{ char name[101]; int age; int grade; }; int partition(struct student *A, int left, int right); void quicksort(struct student *A, int begin, int end); int main() { struct student students[1001]; int i, n; while(scanf("%d",&n) != EOF) { //學(xué)生成績賦值 for(i = 0; i < n; i ++) { scanf("%s%d%d",students[i].name, &students[i].age, &students[i].grade); } //快速排序 quicksort(students, 0, n-1); //打印輸出 for(i = 0; i < n; i ++) { printf("%s %d %d\n",students[i].name, students[i].age, students[i].grade); } } return 0; } void quicksort(struct student *A, int begin, int end) { int pivot; if(begin < end) { pivot = partition(A, begin, end); quicksort(A, begin, pivot - 1); quicksort(A, pivot + 1, end); } } int partition(struct student *A, int left, int right) { struct student stand = A[left]; while(left < right) { while(left < right && (A[right].grade > stand.grade || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) > 0) || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) == 0 && A[right].age > stand.age ) ) ) { right --; } if(left < right) { A[left ++] = A[right]; } while(left < right && (A[left].grade < stand.grade || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) < 0) || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) == 0 && A[left].age < stand.age ) ) ) { left ++; } if(left < right) { A[right --] = A[left]; } } A[left] = stand; return left; }
上一篇:C語言中strspn()函數(shù)和strcspn()函數(shù)的對比使用
欄 目:C語言
下一篇:詳解C語言中的錯誤報告errno與其相關(guān)應(yīng)用方法
本文標(biāo)題:C語言的冒泡排序和快速排序算法使用實例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2871.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什