Advertisement
cd62131

2 lists sort

Jan 17th, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void swap_list(char **list1, char *list2, int i, int j) {
  5.   char *t1;
  6.   char t2;
  7.   t1 = list1[i];
  8.   t2 = list2[i];
  9.   list1[i] = list1[j];
  10.   list2[i] = list2[j];
  11.   list1[j] = t1;
  12.   list2[j] = t2;
  13. }
  14.  
  15. void quick_sort(char **list1, char *list2, int left, int right)
  16. {
  17.   int i, j;
  18.   char *t, *middle;
  19.  
  20.   if (left >= right) return;
  21.   middle = list1[(left + right) / 2];
  22.   for (i = left, j = right;; i++, j--) {
  23.     while (strcmp(list1[i], middle) < 0)
  24.       i++;
  25.     while (strcmp(list1[j], middle) > 0)
  26.       j--;
  27.     if (i >= j) break;
  28.     swap_list(list1, list2, i, j);
  29.   }
  30.   quick_sort(list1, list2, left, i - 1);
  31.   quick_sort(list1, list2, j + 1, right);
  32. }
  33.  
  34. int main(void) {
  35.   char *list1[] = {
  36.     "Tottori", "Kochi", "Nara", "Wakayama", "Kagawa", "Shimane", "Okinawa",
  37.     "Shiga", "Fukui", "Tokushima", "Nagasaki", "Saga", "Yamanashi", "Ehime",
  38.     "Yamaguchi", "Mie", "Oita", "Iwate", "Miyazaki", "Ishikawa", "Toyama",
  39.     "Akita", "Gifu", "Kyoto", "Okayama", "Aomori", "Kumamoto", "Kagoshima",
  40.     "Gunma", "Hyogo", "Fukushima", "Miyagi", "Yamagata", "Nagano", "Hiroshima",
  41.     "Ibaraki", "Tochigi", "Niigata", "Shizuoka", "Osaka", "Saitama", "Aichi",
  42.     "Chiba", "Kanagawa", "Fukuoka", "Hokkaido", "Tokyo"
  43.   };
  44.   char list2[] = "iinauaawrs3aaodr keomaepk s ndoinmdoo diAkaeeh";
  45.   int size = sizeof(list2) / sizeof(char);
  46.   int i;
  47.  
  48.   quick_sort(list1, list2, 0, size - 1);
  49.   for (i = 0; i < size; i++) {
  50.     printf("%s\n", list1[i]);
  51.   }
  52.   printf("%s\n", list2);
  53.   return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement