Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- #include "assert.h"
- #include <algorithm>
- #include <stdio.h>
- int InPut(int *array, int n){
- if(n <= 0)
- return 0;
- int count = 0;
- for(int i = 0; i<n; i++){
- scanf("%d", &array[i]);
- count++;
- }
- int i=0;
- return count;
- }
- int ShowArr(int *array, int n){
- int count = 0;
- if(n <= 0 )
- return 0;
- printf("\n");
- for(int i = 0; i<n; i++){
- printf("%d ", array[i]);
- count++;
- }
- printf("\n");
- return count;
- }
- void swap( int &array[i], &array[j])
- {
- int c = array[i];
- array[i] = array[j];
- array[j] = c;
- }
- void qs(int* array, int first, int last)
- {
- int i = first, j = last, x = array[(first + last) / 2];
- do {
- while (array[i] < x) i++;
- while (array[j] > x) j--;
- if(i <= j) {
- if (array[i] > array[j])
- swap(&array[i],&array[j]);
- i++;
- j--;
- }
- } while (i <= j);
- if (i < last)
- qs(array, i, last);
- if (first < j)
- qs(array, first, j);
- }
- int main()
- {
- int n;
- printf( "Enter a num of elements: ");
- scanf("%d",&n);
- int i=0;
- int j=n;
- int *array = (int*)malloc(sizeof(int)*n);
- InPut(array, n);
- qs(array,0,n);
- ShowArr(array,n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement