Quick sort

I studied Quick sort, I found the algorithm explained here

I understand best, but I have a question at one of the steps.

Could someone correctly explain to me what the steps will be until the rod 57 is placed in the right place, if the number 76 at this point, as shown in the figure, was 7 ?

enter image description here

I think it would be more useful if the reader first saw the steps described in the slides, as I found that there is a lot of other approach for explaining the quick sort algorithm.

Editted: I figured the final sequence would look like

24 49 16 38 55 21 36 9 * 7 * 57 81 85 63 79 74 85 97 61 77 70 * 68. (as mentioned by nullpointer)

, 68 , / ?

+4
2

Contd.. [* ; ** ; *** ] Pivot = 57

=> 24  49  16  38  55  21  36  *68  7  **9  81  85  63  79  74  85  97  61  77  70  ***

=> 24  49  16  38  55  21  36  *9  7  **68  81  85  63  79  74  85  97  61  77  70  ***

, , . 9 68 . 57 7 ( **), , , - 68 ( *).

 => 24  49  16  38  55  21  36  9  **7  *68  81  85  63  79  74  85  97  61  77  70  ***

, , 68 57 . , :

=> 24  49  16  38  55  21  36  9  **7  *57  81  85  63  79  74  85  97  61  77  70  ***68
+3

:

void swap(int *i, int *j)
{
    int t = *i;
    *i = *j;
    *j = t;
}

void QuickSort(int a[], int lo, int hi) {
    int i = lo, j = (lo + hi)/2, k = hi;
    int pivot;
    if (a[k] < a[i])            // median of 3
        swap(a+k, a+i);
    if (a[j] < a[i])
        swap(a+j, a+i);
    if (a[k] < a[j])
        swap(a+k, a+j);
    pivot = a[j];
    showa(lo, hi);
    while (i <= k) {            // partition
        while (a[i] < pivot)
            i++;
        while (a[k] > pivot)
            k--;
        if (i <= k) {
            swap(a+i, a+k);
            i++;
            k--;
            showa(lo, hi);
        }
    }
    if (lo < k)                 // recurse
        QuickSort(a, lo, k);
    if (i < hi)
        QuickSort(a, i, hi);
}

"*" :

57 70 97 38 63 21 85 68 76  9 81 36 55 79 74 85 16 61 77 49 24 
24*70 97 38 63 21 85 68 76  9 57*36 55 79 74 85 16 61 77 49 81*
24 49*97 38 63 21 85 68 76  9 57 36 55 79 74 85 16 61 77 70*81 
24 49 16*38 63 21 85 68 76  9 57 36 55 79 74 85 97*61 77 70 81 
24 49 16 38 55*21 85 68 76  9 57 36 63*79 74 85 97 61 77 70 81 
24 49 16 38 55 21 36*68 76  9 57 85*63 79 74 85 97 61 77 70 81 
24 49 16 38 55 21 36 57*76  9 68*85 63 79 74 85 97 61 77 70 81 
24 49 16 38 55 21 36 57  9*76*68 85 63 79 74 85 97 61 77 70 81 
 9*49 16 38 24*21 36 57 55*                                    
 9 21*16 38 24 49*36 57 55                                     
 9 21 16 24*38*49 36 57 55                                     
 9 21 16 24                                                    
 9 16*21*24                                                    
 9 16                                                          
 9 16                                                          
      21 24                                                    
      21 24                                                    
            36*49 38*57 55                                     
            36 38*49*57 55                                     
            36 38                                              
            36 38                                              
                  49 55*57*                                    
                  49 55 57                                     
                           74*68 85 63 79 76*85 97 61 77 70 81 
                           74 68 70*63 79 76 85 97 61 77 85*81 
                           74 68 70 63 61*76 85 97 79*77 85 81 
                           74 68 70 63 61 76 85 97 79 77 85 81 
                           61*68 70 63 74*                     
                           61 68 63*70*74                      
                           61 63*68*                           
                           61 63 68                            
                                    70 74                      
                                    70 74                      
                                             79*97 81*77 85 85*
                                             79 77*81 97*85 85 
                                             79 77 81 97 85 85 
                                             77*79*            
                                             77 79             
                                                      85*85 97*
                                                      85 85 97 
                                                         85 97 
                                                         85 97 
 9 16 21 24 36 38 49 55 57 61 63 68 70 74 76 77 79 81 85 85 97 
+2

Source: https://habr.com/ru/post/1618277/


All Articles