Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Thursday, August 18, 2011

Quick sort in C

#define maxsize 6


int A[maxsize];



void quicksort(int a, int b)

{

int rtidx=0,ltidx=0,k=a,l=0,pivot;

int leftarr[maxsize],rtarr[maxsize];

pivot=A[a];

if(a==b)return;

while(k

{

++k;

if(A[k]

{

leftarr[ltidx]=A[k];

ltidx++;

}

else

{

rtarr[rtidx]=A[k];

rtidx++;

}

}



k=a;

for(l=0;l

A[k++]=pivot;

for(l=0;l

if(ltidx>0)quicksort(a,a+ltidx-1);

if(rtidx>0)quicksort(b-rtidx+1,b);

}



void printarr(int a)

{

int i;

for(i=0;i

{

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

printf("\n");

}

}



main()

{

int i,s;

printf("enter the number of numbers to be entered \n");

scanf("%d",&s);

for(i=0;i

{

printf("enter the number \n" );

scanf("%d",&A[i]);

}

printf("array before sorting ");

printarr(s);

quicksort(0,s-1);

printf("array after sorting");

printarr(s);

}
 
OUTPUT : -
enter the number of numbers to be entered
5
enter the number
8
enter the number
0
enter the number
-5
enter the number
-1
enter the number
7
array before sorting 8
0
-5
-1
7
array after sorting -5
-1
0
7
8
 
 
 
r u unable to understand this program??????????TRY THESE










1. Email us at chelperr@gmail.com



2. Post your question in the comment box.



3. You can give us your email ID also.

4. If you want to ask a program/topic or wanted introduce any discussion topic then just email at chelperr@gmail.com or drop a comment in the comment box.

Sunday, August 14, 2011

Bubble sorting in C

#include "stdio.h"


#include "conio.h"

void main()

{

int arr[100],temp,i,n,j;

clrscr();

printf("\nEnter number of elements in array less Than 100 : ");

scanf("%d",&n);

printf("\n\tEnter The Values into ARRAY ");

for(i=0;i

{

printf("\n\n Enter Element no %d: ",i+1);

scanf("%d",&arr[i]);

}

for(i=0;i

{

for(j=0;j

{

if(arr[j] >arr[j+1])

{

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

}

}

}

printf("\n\n-- Sorted Series --");

for(i=0;i

{

printf("\n \n \t %d",arr[i]);

}

getch();





}
 
OUTPUT : -
 
Enter number of elements in array less than 100 : 5
                   
                            Enter the values into ARRAY
Enter element no 1 : 8
 
Enter element no 2 : -6
 
Enter element no 3 : 0
 
Enter element no 4 : 1
 
Enter element no 5: 9
 
--Sorted Series--
         -6
          0
          1
          8
          9