Advertisement
cd62131

SelectionSortOnList

Jul 16th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. typedef struct list_t list_t;
  5. struct list_t {
  6.   int element;
  7.   list_t *next;
  8. };
  9. static int random_int(int upper);
  10. static int *new_random_array(int n);
  11. static void puts_array(int *a, int n);
  12. static list_t *new_cell(int element);
  13. static list_t *make_list(int n);
  14. static void puts_list(list_t *head);
  15. static void selection_sort_recur(int *a, int n);
  16. static void selection_sort_on_list_recur(list_t **head);
  17. int main(void);
  18. static int random_int(int upper) {
  19.   return (int) ((rand() / ((double) RAND_MAX + 1.)) * upper);
  20. }
  21. static int *new_random_array(int n) {
  22.   int i;
  23.   int *a = (int *) malloc(n * sizeof(int));
  24.   for (i = 0; i < n; i++)
  25.     a[i] = random_int(100);
  26.   return a;
  27. }
  28. static void puts_array(int *a, int n) {
  29.   int i;
  30.   printf("[");
  31.   for (i = 0; i < n; i++) {
  32.     printf("%d", a[i]);
  33.     if (i < n - 1) printf(", ");
  34.   }
  35.   printf("]\n");
  36. }
  37. static list_t *new_cell(int element) {
  38.   list_t *new = (list_t *) malloc(sizeof(list_t));
  39.   new->element = element;
  40.   new->next = NULL;
  41.   return new;
  42. }
  43. static list_t *make_list(int n) {
  44.   int i;
  45.   list_t *head, *p;
  46.   for (i = 0; i < n; i++) {
  47.     if (i == 0) {
  48.       head = new_cell(random_int(100));
  49.       continue;
  50.     }
  51.     p = new_cell(random_int(100));
  52.     p->next = head;
  53.     head = p;
  54.   }
  55.   return head;
  56. }
  57. static void puts_list(list_t *head) {
  58.   list_t *p;
  59.   printf("[");
  60.   for (p = head; p; p = p->next) {
  61.     printf("%d", p->element);
  62.     if (p->next) printf(", ");
  63.   }
  64.   printf("]\n");
  65. }
  66. static void selection_sort_recur(int *a, int n) {
  67.   int i = 0, j, min = a[0];
  68.   if (n == 1) return;
  69.   for (j = 1; j < n; j++)
  70.     if (a[j] < min) {
  71.       min = a[j];
  72.       i = j;
  73.     }
  74.   if (i != 0) {
  75.     a[i] = a[0];
  76.     a[0] = min;
  77.   }
  78.   selection_sort_recur(a + 1, n - 1);
  79. }
  80. static void selection_sort_on_list_recur(list_t **head) {
  81.   list_t *p, *min = *head, *prev, *prev_min = *head;
  82.   puts_list(*head);
  83.   if (!(*head)->next) return;
  84.   for (prev = *head, p = (*head)->next; p; prev = p, p = p->next)
  85.     if (p->element < min->element) {
  86.       min = p;
  87.       prev_min = prev;
  88.     }
  89.   if (min != *head) {
  90.     prev_min->next = *head;
  91.     p = min->next;
  92.     min->next = (*head)->next;
  93.     (*head)->next = p;
  94.     *head = min;
  95.   }
  96.   selection_sort_on_list_recur(&((*head)->next));
  97. }
  98. int main(void) {
  99.   const int n = 10;
  100.   int *a;
  101.   list_t *head;
  102.   srand((unsigned) time(NULL));
  103.   puts("Array:");
  104.   a = new_random_array(n);
  105.   puts_array(a, n);
  106.   selection_sort_recur(a, n);
  107.   puts_array(a, n);
  108.   puts("List:");
  109.   head = make_list(n);
  110.   puts_list(head);
  111.   selection_sort_on_list_recur(&head);
  112.   puts_list(head);
  113.   return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement