SHARE
TWEET

sort sliyaniem

allekco Oct 17th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <stddef.h>
  8.  
  9. void sort (int *A, int a1, int a2){
  10.     if (a1==a2) return;
  11.     int middle;
  12.     middle = (a2 + a1)/2;
  13.     sort (A, a1, middle);
  14.     sort (A, middle+1, a2);
  15.     merge (A, a1, a2, middle);
  16. }
  17.  
  18. void merge (int *A, int a1, int a2, int middle){ //ñëèÿíèå
  19.     int i = a1;
  20.     int j = middle+1;
  21.     int k;
  22.     int *B;
  23.     B = (int*)malloc(a2 * sizeof(int));
  24.     for (k = 0; k < a2-a1+1; k++){
  25.         if (((i <= middle) && (A[i] < A[j])) || (j > a2)){
  26.             B[k] = A[i];
  27.             i++;
  28.         }
  29.         else {
  30.             B[k] = A[j];
  31.             j++;
  32.         }
  33.     }
  34.     for (k = 0; k < a2-a1+1; k++){
  35.          A[a1 + k] = B[k];
  36.     }
  37. }
  38.  
  39. int main (void) {
  40.     int N, i;
  41.     scanf ("%d", &N);
  42.     int A[N];
  43.     float random;
  44.     srand(time(NULL)); 
  45.    
  46.     for (i = 0; i < N; i++){
  47.         random = rand();
  48.         random = (random / RAND_MAX)*100;
  49.         A[i] = random;
  50.     //  scanf ("%d", &A[i]);
  51.     }  
  52.     for (i=0; i<N; i++){
  53.         printf ("%d ", A[i]);      
  54.     }
  55.     printf (" \n");
  56.     printf (" \n");
  57.    
  58.     clock_t start = clock() ;
  59.     sort (A, 0, N-1);      
  60.     clock_t end= clock ( ) ;
  61.     float seconds = (float) (end - start) / CLOCKS_PER_SEC;          
  62.    
  63.     for (i = 0; i < N; i++){
  64.         printf ("%d ", A[i]);      
  65.     }  
  66.     printf (" \n");
  67.     printf ("%f ", seconds);
  68.    
  69.     return 0;
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top