C Program to implement quick sort

C Program to implement quick sort. Quick sort algorithm is based on divide and conquer strategy. In a quick sort we take the one element called as pivot,then we list all the smaller elements than pivot, and greater than pivot. after partitioning we have pivot in the final position. After recursively sorting the partition array, we get the sorted elements. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>

void swap (int a[], int left, int right)
{
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}//end swap

void quicksort( int a[], int low, int high )
{
int pivot;
// Termination condition!
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
} //end quicksort

int partition( int a[], int low, int high )
{
int left, right;
int pivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right )
{
// Move left while item < pivot
while( a[left] <= pivot_item )
left++;
// Move right while item > pivot
while( a[right] > pivot_item )
right--;
if ( left < right )
swap(a,left,right);
}
// right is final position for the pivot
a[low] = a[right];
a[right] = pivot_item;
return right;
}//end partition

// void quicksort(int a[], int, int);
void printarray(int a[], int);

int main()
{
int a[50], i, n;
printf("nEnter no. of elements: ");
scanf("%d", &n);
printf("nEnter the elements: n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("nUnsorted elements: n");
printarray(a,n);
quicksort(a,0,n-1);
printf("nSorted elements: n");
printarray(a,n);

}//end main


void printarray(int a[], int n)
{
int i;
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("n");
}//end printarray

Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to accept N numbers and arrange them in an descending order

C Sorting,
C program to accept N numbers and arrange them in an descending order. Program will accept the array of elements, and returns the elements in descending order. Program use the Bubble sort Technique to sort the Array. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>

void main ()
{
int i,j,a,n,number[30];

printf ("Enter the value of Nn");
scanf ("%d", &n);

printf ("Enter the numbers n");
for (i=0; i<n; ++i)
scanf ("%d",&number[i]);

for (i=0; i<n; ++i)
{
for (j=i+1; j<n; ++j)
{
if (number[i] < number[j])
{
a= number[i];
number[i] = number[j];
number[j] = a;
}
}
}

printf ("The numbers arrenged in descending order are given belown");
for (i=0; i<n; ++i)
printf ("%dn",number[i]);
} /* End of main() */
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to sort given N elements using SELECTION sort method

Write a C program to sort given N elements using SELECTION sort method using functions :
a) To find maximum of elements
b) To swap two elements
Selection sort is comparison based sorting technique, It finds the minimum value in list and swaps that value to the first position and so on. Selection sort is inefficient on larger data. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>

void main()
{
int array[10];
int i, j, N, temp;

int findmax(int b[10], int k); /* function declaration */
void exchang(int b[10], int k);

clrscr();

printf("Enter the value of Nn");
scanf("%d",&N);

printf("Enter the elements one by onen");
for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);

}

printf("Input array elementsn");
for(i=0; i<N ; i++)
{
printf("%dn",array[i]);
}

/* Selection sorting begins */
exchang(array,N);

printf("Sorted array is...n");
for(i=0; i< N ; i++)
{
printf("%dn",array[i]);
}

} /* End of main*/

/* function to find the maximum value */
int findmax(int b[10], int k)
{
int max=0,j;
for(j = 1; j <= k; j++)
{
if ( b[j] > b[max])
{
max = j;
}
}
return(max);
}


void exchang(int b[10],int k)
{
int temp, big, j;
for ( j=k-1; j>=1; j--)
{

big = findmax(b,j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
}
return;
}
Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program To Sort Names

Write a C program to read N names, store them in the form of an array and sort them in alphabetical order.Output the given names and the sorted names in two columns side by side with suitable heading.Program will sort the name strings using Bubble Sort technique. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;

clrscr();

printf("Enter the value of Nn");
scanf("%d", &N);

printf("Enter %d namesn", N);
for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}

for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}

printf("n----------------------------------------n");
printf("Input NamestSorted namesn");
printf("------------------------------------------n");
for(i=0; i< N ; i++)
{
printf("%stt%sn",Tname[i], name[i]);
}
printf("------------------------------------------n");

} /* End of main() */
Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program To Sort An Array Using Bubble Sort

C Program To Sort An Array Using Bubble Sort. Bubble Sort is the simplest and easiest sorting technique. In this technique, the two successive items A[i] and A[i+1] are exchanged whenever A[i]>=A[i+1]. The larger values sink to the bottom of the array and hence it is called sinking sort. The end of each pass smaller values gradually “bubble” their way upward to the top(like air bubbles moving to surface of water) and hence called bubble sort. Read more about C Programming Language .

Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

Binary search in C

C Program to implement Binary search. Binary search technique is simple searching technique which can be applied if the items to be compared are either in ascending order or descending order. The general idea used in binary search is similar to the way we search for the telephone number of a person in the telephone directory. Binary search is the divide and conquer strategy.Read more about C Programming Language.
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* This program was originally published at
* http://www.c-program-example.com/2011/09/c-program-for-binary-search.html
* Happy Coding
***********************************************************/
/*C Program for Binary search */
#include<stdio.h>
int main() {
int n, a[30], item, i, j, mid, top, bottom;
printf("Enter how many elements you want:\n");
scanf("%d", &n);
printf("Enter the %d elements in ascending order\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the item to search\n");
scanf("%d", &item);
bottom = 1;
top = n;
do {
mid = (bottom + top) / 2;
if (item < a[mid])
top = mid - 1;
else if (item > a[mid])
bottom = mid + 1;
} while (item != a[mid] && bottom <= top);
if (item == a[mid]) {
printf("Binary search successfull!!\n");
printf("\n %d found in position: %d\n", item, mid + 1);
} else {
printf("\n Search failed\n %d not found\n", item);
}
return 0;
}
view raw binary_search.c hosted with ❤ by GitHub

Read more Similar C Programs
C Basic
Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com