Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- void swap(int *a,int *b)
- {
- int temp=*a;
- *a=*b;
- *b=temp;
- }
- int partition(int arr[],int start,int end)
- {
- int pivot=arr[end];
- int i=start-1,j;
- for (j=start;j<end;j++)
- {
- if (arr[j]<=pivot)
- {
- i++;
- swap(&arr[i], &arr[j]);
- }
- }
- swap(&arr[i + 1], &arr[end]);
- return (i + 1);
- }
- void QuickUtil(int arr[],int start,int end)
- {
- if(start<end)
- {
- int pivot=partition(arr,start,end);
- QuickUtil(arr,start,pivot-1);
- QuickUtil(arr,pivot+1,end);
- }
- }
- //Quick Sort
- void my_sort3(int arr[],int n)
- {
- QuickUtil(arr,0,n-1);
- }
- int main() {
- int n, i;
- scanf("%d", &n);
- int *a = malloc(sizeof(int) * n);
- srand(time(NULL));
- for(i = 0; i < n; ++i) {
- a[i] = rand() % 10000;
- }
- my_sort3(a,n);
- int sorted = 1;
- for(i = 0; i < n - 1; ++i) {
- if(a[i] > a[i + 1]) {
- sorted = 0;
- break;
- }
- }
- if(!sorted) {
- printf("NOT SORTED");
- } else {
- printf("SORTED");
- }
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement