- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49635 个
- 通用积分
- 55.6937
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57805 点
- 帖子
- 4005
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
|
62楼
ReneeBK(真实交易用户)
发表于 2016-1-11 08:18:21
- // Program P8.x Parallel sort
- #include <stdio.h>
- #include <string.h>
- #define MaxNameSize 14
- #define MaxNameBuffer MaxNameSize+1
- #define MaxNames 8
- int main() {
- void parallelSort(int, int, int max, char [][max], int[]);
- char name[MaxNames][MaxNameBuffer] = {"Samlal, Rawle", "Williams, Mark",
- "Delwin, Mac", "Taylor, Victor", "Mohamed, Abu",
- "Singh, Krishna", "Tawari, Tau", "Abdool, Alana" };
- int id[MaxNames] = {8742,5418,4833,4230,8583,2458,5768,3313};
- parallelSort(0, MaxNames-1, MaxNameBuffer, name, id);
- printf("\nThe sorted names and IDs are\n\n");
- for (int h = 0; h < MaxNames; h++) printf("%-18s %d\n", name[h], id[h]);
- } //end main
- void parallelSort(int lo, int hi, int max, char list[][max], int id[]) {
- //Sort the names in list[lo] to list[hi] in alphabetical order, ensuring
- //that each name remains with its original id number.
- //The maximum string size is max - 1 (one char taken up by \0).
- char key[max];
- for (int h = lo + 1; h <= hi; h++) {
- strcpy(key, list[h]);
- int m = id[h]; // extract the id number
- int k = h - 1; //start comparing with previous item
- while (k >= lo && strcmp(key, list[k]) < 0) {
- strcpy(list[k + 1], list[k]);
- id[k+ 1] = id[k]; // move up id number when we move a name
- --k;
- }
- strcpy(list[k + 1], key);
- id[k + 1] = m; // store the id number in the same position as the name
- } //end for
- } //end parallelSort
复制代码
|
|