Advertisement
cd62131

SelectionSortOnList

Jul 16th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 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.     selection_sort_recur(a + 1, n - 1);
  78.   }
  79. }
  80. static void selection_sort_on_list_recur(list_t **head) {
  81.   list_t *p, *min = *head, *prev, *prev_min = *head;
  82.   if (!(*head)->next) return;
  83.   for (prev = *head, p = (*head)->next; p; prev = p, p = p->next)
  84.     if (p->element < min->element) {
  85.       min = p;
  86.       prev_min = prev;
  87.     }
  88.   if (prev_min != *head) {
  89.     prev_min->next = *head;
  90.     p = min->next;
  91.     min->next = (*head)->next;
  92.     (*head)->next = p;
  93.     *head = min;
  94.     selection_sort_on_list_recur(&((*head)->next));
  95.   }
  96. }
  97. int main(void) {
  98.   const int n = 10;
  99.   int *a;
  100.   list_t *head;
  101.   srand((unsigned) time(NULL));
  102.   puts("Array:");
  103.   a = new_random_array(n);
  104.   puts_array(a, n);
  105.   selection_sort_recur(a, n);
  106.   puts_array(a, n);
  107.   puts("List:");
  108.   head = make_list(n);
  109.   puts_list(head);
  110.   selection_sort_on_list_recur(&head);
  111.   puts_list(head);
  112.   return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement