Advertisement
leo99fi

Lab 7 - 4.Напредно сортирање

Dec 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. /*Напредно сортирање
  2. Да се напише функција за ефикасно сортирање на низа. Низата може да има 100000 елементи.*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. void quicksort(int a[],int first,int last){
  8.     int pivot,j,temp,i;
  9.  
  10.      if(first<last){
  11.          pivot=first;
  12.          i=first;
  13.          j=last;
  14.  
  15.          while(i<j){
  16.              while(a[i]<=a[pivot]&&i<last)
  17.                  i++;
  18.              while(a[j]>a[pivot])
  19.                  j--;
  20.              if(i<j){
  21.                  temp=a[i];
  22.                   a[i]=a[j];
  23.                   a[j]=temp;
  24.              }
  25.          }
  26.  
  27.          temp=a[pivot];
  28.          a[pivot]=a[j];
  29.          a[j]=temp;
  30.          quicksort(a,first,j-1);
  31.          quicksort(a,j+1,last);
  32.  
  33.     }
  34. }
  35.  
  36. void my_sort(int *a, int n) {
  37.         // вашата функција за сортирање
  38.     quicksort(a,0,n-1);
  39. }
  40.  
  41. // не ја менувајте главната функција
  42. int main() {
  43.     int n, i;
  44.     scanf("%d", &n);
  45.     int *a = malloc(sizeof(int) * n);
  46.     srand(time(NULL));
  47.     for(i = 0; i < n; ++i) {
  48.         a[i] = rand() % 10000;
  49.     }
  50.    
  51.    
  52.     my_sort(a, n);
  53.    
  54.  
  55.     int sorted = 1;
  56.     for(i = 0; i < n - 1; ++i) {
  57.         if(a[i] > a[i + 1]) {
  58.             sorted = 0;
  59.             break;
  60.         }
  61.     }
  62.     if(!sorted) {
  63.         printf("NOT SORTED");
  64.     } else {
  65.         printf("SORTED");
  66.     }
  67.     free(a);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement