-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedQuicksort.java
More file actions
73 lines (63 loc) · 1.72 KB
/
RandomizedQuicksort.java
File metadata and controls
73 lines (63 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cs501l_hw5_vinayakgadad_15226;
/**
*
* @author Vinayak
*/
public class RandomizedQuicksort {
int[] array=new int[]{4,1,3,2,16,9,10,14,8,7};
public static void main(String[] args)
{
RandomizedQuicksort ranqs = new RandomizedQuicksort();
System.out.println("The original input array");
ranqs.original();
ranqs.sort();
System.out.println("\n\nThe final sorted array after performing the Quicksort algorithm");
ranqs.original();
}
public void sort()
{
randomizedPartition(array, 0, array.length-1);
}
public void swap(int a, int b)
{
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
public void randomizedPartition(int array[],int first,int last)
{
int mid;
if (first<last) {
int pivot=array[last];
int i=first-1;
for(int j=first;j<=last-1;j++){
if(array[j]<pivot){
i=i+1;
swap(i,j);
}
}
swap(i+1,last);
mid=i+1;
System.out.println("\n The sub-arrays after each of the randomizedPartition calls has completed");
inputArray(first,mid-1);
inputArray(mid+1,last);
randomizedPartition(array,first,mid-1);
randomizedPartition(array,mid+1,last);
}
}
public void inputArray(int m, int n)
{
for(int j=m; j<=n; j++)
System.out.print(array[j] + " ");
}
public void original()
{
inputArray(0,array.length-1);
System.out.println("");
}
}