Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
RANDOMISED QUICKSORT

RANDOMISED QUICKSORT

Ratings: (0)|Views: 160 |Likes:
Published by noeldavid22

More info:

Published by: noeldavid22 on Mar 17, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

08/03/2012

pdf

text

original

 
ALGORITHM ANALYSIS AND DEISGNASSIGNMENT: RANDOMISED QUICK SORT
SUBMITTED BY:NOEL DAVID EJS3 MCA918
 
RANDOMISED QUICKSORTAIM
To implement randomized quick sort.
DESCRIPTION
The basic version of quick sort algorithm was invented by C. A. R. Hoare in 1960and formally introduced quick sort in 1962. It is used on the principle of divide-and-conquer.
 
Quick sort works by partitioning a given array
A
[
p
. .
] into two non-empty subarray
A
[
p
. .
q
] and
A
[
q
+1 . .
] such that every key in
A
[
p
. .
q
] is less than or equal toevery key in
A
[
q
+1 . .
]. Then the two sub arrays are sorted by recursive calls to Quick sort.
 
In the randomized version of Quick sort we impose a distribution on input. This doesnot improve the worst-case running time independent of the input ordering.
 
In this versionwe choose a random key for the pivot. Assume that procedure Random (
p, q
) returns arandom integer in the range [
p, q
); there are
q
-
p
+1 integers in the range and procedure isequally likely to return one of them. The new partition procedure, simply implemented theswap before actually partitioning.
ALGORITHM
RANDOMIZED_QUICKSORT
(
A, p, r 
)
If p < r thenq ← RANDOMIZED_PARTITION (
A, p, r 
)RANDOMIZED_QUICKSORT (
A, p, q
)RANDOMIZED_QUICKSORT (
A, q+1, r 
)RANDOMIZED_PARTITION (
A, p, r 
)
i
← RANDOM (
p, r 
)Exchange
A
[
p
]
 
A
[
i
]return PARTITION (
A, p, r 
)
PROGRAM
#include<stdio.h>#include<conio.h>#include<time.h>int partition(int [],int ,int );void quick_sort(int [],int ,int );void disparray(int [],int,int,int);int n;void main()
 
{int array[30],i,k;time_t first,second;clrscr();printf("Quick Sort Implementation\n");printf("-------------------------\n");printf("Enter the size of array: ");scanf("%d",&n);printf("\nEnter the elements:\n");printf("---------------------\n");for(i=1;i<=n;i++)scanf("%d",&array[i]);clrscr();printf(" R A N D O M I Z E D Q U I C K S O R T \n");printf("--------------------------------------------------\n");first=time(NULL);quick_sort(array,1,n);second=time(NULL);printf("Sorted Array:\n");printf("---------------------------------------------------\n");for(k=1;k<=n;k++)printf("%d\t",array[k]);printf("\n\nTime taken for sorting: %f “,difftime(second,first));getch();return;}int random (int p, int r){return(p+rand()%(r-p+1));}void quick_sort(int array[],int p,int r){int q;if(p<r){q=partition(array,p,r);quick_sort(array,p,q-1);quick_sort(array,q+1,r);}}int partition(int array[],int p,int r){int j,pos,x,k;int i=p-1,tmp;pos=random(p,r);for(k=1;k<=n;k++)printf("%d\t",array[k]);printf("\n\nPivot: %d Position: %d\n\n",array[pos],pos);getch();tmp=array[pos]; array[pos]=array[r]; array[r]=tmp;x=array[r];for(j=p;j<=r-1;j++)if(array[j]<=x){

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->