Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 12
- //to sort array, so it goes as follows: 12,10,8,6,4,2,1,3,5,7,9,11
- //works fine - with even and neven SIZE, with if/else and with trenary instead,
- //with max_index=s/2 as well
- void pr_arr(int s,int *a);
- void sort_arr(int s,int *a);
- void swap_el(int *a,int ind1,int ind2);
- int main()
- {
- int arr[SIZE]= {1,2,10,3,9,4,8,5,12,6,7,11};
- pr_arr(SIZE,arr);
- sort_arr(SIZE,arr);
- pr_arr(SIZE,arr);
- return 0;
- }
- void pr_arr(int s,int *a)
- {
- int i;
- for(i=0;i<s;i++)
- {
- printf("%d\t",a[i]);
- }
- printf("\n\n");
- }
- void sort_arr(int s,int *a)
- {
- int temp,i,j,index_max,up,down =-1;//i-counter,temp is value, everything else
- // is indexes
- up = s, index_max = s/2;
- for(i=0;i<s;i++)//i -just a cycle counter
- {
- /*if(!(i%2))//i is even, SO we use down as target index
- index_max=++down;
- else// i is not even,SO we use up as target index
- index_max=--up;*/
- (!(i%2))? ++down: --up;
- //(!(i%2))? index_max=down:--up,index_max=up;
- printf("\ni %d\t,down %d\t,up %d\t,index_max %d\n",i,down,up,index_max);
- for(j=down;j<=up;j++)//looking for the max element between down & up
- {
- //printf("j is %d\t",j);
- if(a[j]>a[index_max])
- swap_el(a,j,index_max);
- }
- //swaps a[down] or a[up](depending on i)
- //with a[index_max], OR swaps target element with max element
- (i%2)? swap_el(a,down,index_max):swap_el(a,up,index_max);
- pr_arr(s,a);
- }
- }
- void swap_el(int *a,int ind1,int ind2)
- {
- int temp = a[ind1];
- a[ind1]= a[ind2];
- a[ind2]= temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement