Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include "iostream"
  2. #include "assert.h"
  3. #include <algorithm>
  4. #include <stdio.h>
  5.  
  6.  
  7.  
  8. int InPut(int *array, int n){
  9.         if(n <= 0)
  10.         return 0;
  11.         int count = 0;
  12.         for(int i = 0; i<n; i++){
  13.                 scanf("%d", &array[i]);
  14.                 count++;
  15.         }
  16.         int i=0;
  17.         return count;
  18. }
  19.  
  20. int ShowArr(int *array, int n){
  21.         int count = 0;
  22.         if(n <= 0 )
  23.         return 0;
  24.         printf("\n");
  25.         for(int i = 0; i<n; i++){
  26.                 printf("%d ", array[i]);
  27.                 count++;
  28.         }
  29.         printf("\n");
  30.         return count;
  31. }
  32. void swap( int &array[i], &array[j])
  33. {
  34. int c = array[i];
  35. array[i] = array[j];
  36. array[j] = c;
  37. }
  38.  
  39. void qs(int* array, int first, int last)
  40. {
  41.     int i = first, j = last, x = array[(first + last) / 2];
  42.  
  43.     do {
  44.         while (array[i] < x) i++;
  45.         while (array[j] > x) j--;
  46.  
  47.         if(i <= j) {
  48.             if (array[i] > array[j])
  49.             swap(&array[i],&array[j]);
  50.             i++;
  51.             j--;
  52.         }
  53.     } while (i <= j);
  54.  
  55.     if (i < last)
  56.         qs(array, i, last);
  57.     if (first < j)
  58.         qs(array, first, j);
  59. }
  60.  
  61.  
  62.  
  63. int main()
  64. {
  65.         int n;
  66.         printf( "Enter a num of elements: ");
  67.         scanf("%d",&n);
  68.         int i=0;
  69.         int j=n;
  70.         int *array = (int*)malloc(sizeof(int)*n);
  71.         InPut(array, n);
  72.         qs(array,0,n);
  73.         ShowArr(array,n);
  74.         return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement