c - Figuring out the function of cascade of ternary operators? -


i'm trying implement simple (and easy understand) version of merge sort , after bit of research i found following c version:

#include <stdio.h> #include <stdlib.h>  void merge (int *a, int n, int m)  {     //...     int i, j, k;      // allocate memory on heap temp array     int *x = malloc(n * sizeof (int));      // happening here (sorting)     (i = 0, j = m, k = 0; k < n; k++)      {         x[k] = j == n      ? a[i++]              : == m      ? a[j++]              : a[j] < a[i] ? a[j++]              :               a[i++];     }      // deep copy of temp elements initial array     (i = 0; < n; i++)      {         a[i] = x[i];     }      // free temp array     free(x); }  //--------------------------------------------------------------------- void merge_sort (int *a, int n)  {     // base case (subarray 2 elements)     if (n < 2)     {         return;     }      // find index of middle element     int m = n / 2;      // divide 2     merge_sort(a, m);     merge_sort(a + m, n - m);      // sort , merge     merge(a, n, m); }  //===================================================================== int main () {     int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};     int n = sizeof / sizeof a[0];     int i;     (i = 0; < n; i++)     {         printf("%d%s", a[i], == n - 1 ? "\n" : " ");     }      merge_sort(a, n);      (i = 0; < n; i++)     {         printf("%d%s", a[i], == n - 1 ? "\n" : " ");     }      return 0; }  

could me describe , comment (possibly break if statements) long cascade of ternary operators in for loop in function merge?

content available under gnu free documentation license 1.2 unless otherwise noted.

what didn't catch has nothing specific merge sort:

    x[k] = j == n      ? a[i++]          : == m      ? a[j++]          : a[j] < a[i] ? a[j++]          :               a[i++]; 

has been written prefer sexy/funny looking code on readiness. real code have lines. could/should expressed as:

if(j == n) {     x[k] = a[i++]; } else if(i == m) {     x[k] = a[j++]; } else if(a[j] < a[i]) {     x[k] = a[j++]; } else {     x[k] = a[i++]; } 

if need understand how merge sort works see merge sort on wikipedia:

enter image description here


i've modified bit code old fashion debugging, , used numbers of gif can follow, first here's output:

pass 1. n:2, m:1 inserting 5 inserting 6  pass 2. n:2, m:1 inserting 1 inserting 3  pass 3. n:4, m:2 inserting 1 inserting 3 inserting 5 inserting 6  pass 4. n:2, m:1 inserting 7 inserting 8  pass 5. n:2, m:1 inserting 2 inserting 4  pass 6. n:4, m:2 inserting 2 inserting 4 inserting 7 inserting 8  pass 7. n:8, m:4 inserting 1 inserting 2 inserting 3 inserting 4 inserting 5 inserting 6 inserting 7 inserting 8 

and now, code:

#include <stdio.h> #include <stdlib.h>  static int pass = 0; void merge (int *a, int n, int m) {     int i, j, k;     int *x = malloc(n * sizeof (int));      printf("\npass %d. n:%d, m:%d\n", ++pass, n, m);      (i = 0, j = m, k = 0; k < n; k++) {         if(j == n)         {             printf("inserting %d\n", a[i]);             x[k] = a[i++];         }         else if(i == m)         {             printf("inserting %d\n", a[j]);             x[k] = a[j++];         }         else if(a[j] < a[i])         {             printf("inserting %d\n", a[j]);             x[k] = a[j++];         }         else         {             printf("inserting %d\n", a[i]);             x[k] = a[i++];         }     }     (i = 0; < n; i++) {         a[i] = x[i];     }     free(x); }  void merge_sort (int *a, int n) {     if (n < 2)         return;     int m = n / 2;     merge_sort(a, m);     merge_sort(a + m, n - m);     merge(a, n, m); }  int main () {     int a[] = {6, 5, 3, 1, 8, 7, 2, 4};     int n = sizeof / sizeof a[0];     int i;     (i = 0; < n; i++)         printf("%d%s", a[i], == n - 1 ? "\n" : " ");     merge_sort(a, n);     (i = 0; < n; i++)         printf("%d%s", a[i], == n - 1 ? "\n" : " ");     return 0; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -