Advertisement
Guest User

Код сортировки не рабочий

a guest
Nov 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 12
  3.  
  4. //suppose to sort array, so it goes as follows: 12,10,8,6,4,2,1,3,5,7,9,11
  5. //instead it goes like that: 11,9,7,5,3,1,2,4,6,8,10,12
  6. //can't figure out where i'm mistaken?
  7.  
  8. // печать массива длиной s и указателем на него a
  9. void pr_arr(int s, int *a);
  10. // сортировка массива длиной s и указателем на него a
  11. void sort_arr(int s, int *a);
  12.  
  13. int main()
  14. {    
  15.     int arr[SIZE] = {1,2,10,3,9,4,8,5,12,6,7,11};  
  16.          
  17.     pr_arr(SIZE, arr); // печатаем массив
  18.     sort_arr(SIZE, arr); // сортируем массив
  19.     pr_arr(SIZE, arr); // распечатаем полученный массив
  20.  
  21.     return 0;
  22. }
  23.  
  24.  
  25.  
  26. // печать массива длиной s и указателем на него a
  27. void pr_arr(int s, int *a)
  28. {
  29.     int i;
  30.     for(i=0; i<s; i++) printf("%d  ", a[i]);    
  31.     printf("\n\n");
  32. }
  33.  
  34.  
  35. // обмен элементов в массиве по указателю a (ind - индексы для обмена)
  36. inline void swap_el(int *a, int ind1, int ind2)
  37. {
  38.     int temp = a[ind1];
  39.     a[ind1] = a[ind2];
  40.     a[ind2] = temp;
  41. }
  42.  
  43. // сортировка массива длиной s и указателем на него a
  44. void sort_arr(int s, int *a)
  45. {
  46.     int temp, i, j, index_max, up, down=-1;
  47.         //down - target index for even i cycles,it
  48.     //covers first 6 elements of an array a[0] to a[5],up - for uneven cycles
  49.     //i/down 0/0, 2/1,  4/2, 6/3, 8/4, 10/5
  50.     //i/up   1/11,3/10, 5/9, 7/8, 9/7, 11/6
  51.     up = s-1;
  52.     i=0;
  53.  
  54.    for(i=0; i<s; i++) //i -just a cycle counter
  55.    {
  56.        if( !(i%2) ) //i is even, SO target index now is down
  57.         index_max = ++down;
  58.        else //i is not even,SO target index now is up
  59.         index_max = --up;
  60.  
  61.        //(!(i%2))? ++down,index_max=down:--up,index_max=up;//almost good, only order
  62.        //11,9,..1,2..12 (instead of 12,10..2,1..11),or starts with a[11] not a[0]
  63.  
  64.        printf("\ni %d\t,down %d\t,up %d\t,index_max %d\n", i, down, up, index_max);
  65.        for(j=down; j<=up; j++)//looking for the max element between down & up
  66.        {
  67.            //printf("j is %d\t",j);
  68.            if( a[j] > a[index_max] )
  69.             swap_el(a, j, index_max); // обмен элементов
  70.        }
  71.        //swaps a[down] or a[up](depending on i)
  72.        //with a[index_max], OR swaps target element with max element
  73.        (i%2) ? swap_el(a, down, index_max)
  74.              : swap_el(a, up, index_max);                    
  75.        pr_arr(s,a); // печать массива
  76.    }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement