0% found this document useful (0 votes)
80 views5 pages

Assignment On Quick Sort Program in C Program

Assignment on Quick sort program in C program. It can be use for university documentation and any important publications.

Uploaded by

Mosabbir Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views5 pages

Assignment On Quick Sort Program in C Program

Assignment on Quick sort program in C program. It can be use for university documentation and any important publications.

Uploaded by

Mosabbir Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Program Quick Sort:

#include <stdio.h>

#include<conio.h>

void quickSort( int[], int, int);

int partition( int[], int, int);

void main()

int a[] = { 70, 12, 10, 40, 60, 15, 4, 11, 9};

int i;

printf("\n\nUnsorted array is: ");

for(i = 0; i < 9; ++i)

printf(" %d ", a[i]);

quickSort( a, 0, 8);

printf("\n\nSorted array is: ");

for(i = 0; i < 9; ++i)

printf(" %d ", a[i]);

void quickSort( int a[], int l, int r)

{
int j;

if( l < r )

// divide and conquer

j = partition( a, l, r);

quickSort( a, l, j-1);

quickSort( a, j+1, r);

int partition( int a[], int l, int r) {

int pivot, i, j, t;

pivot = a[l];

i = l; j = r+1;

while( 1)

do ++i; while( a[i] <= pivot && i <= r );

do --j; while( a[j] > pivot );

if( i >= j ) break;

t = a[i]; a[i] = a[j]; a[j] = t;

t = a[l]; a[l] = a[j]; a[j] = t;

return j;

}
OUTPUT:
C Program for Binary search
#include <stdio.h>

#include <conio.h>

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d",&array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;
}

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

printf("Not found! %d is not present in the list.\n", search);

return 0;

OUTPUT:

You might also like