My Quicksort technique will help students.
QuickSort:
Divide and Conquer type. Sorting set is divided into sorting 2 smaller sets.
Find final positon for each pivotal and give 2 sublists ( sublist1 < pivotal < sublist2) like this way.
Reduction step of the this algorithm finds the final position of the numbers
means
we are starting with 44 using quicksort technique we will find the final position of 44th in the list
This is simple, we will right to left scan and left to right scan
pivotal number: 44, we are finding final position for this number lets see ?
Current position of 44: 0, after quicksort we will find the correct position for this 44.
Final Position of 44: ?
Ex: Suppose A is the list of 12 numbers.
44 33 11 55 77 90 40 60 99 22 88 66
-> right to left scan: ( search from right to check until number < 44), Start:66, Pivotal:44
First Number 44, last number 66, scan the list from right to left.
comparing each number with 44 and stopping at the number < 44
That is 22. Interchange 44 and 22 to obtain the list
22 33 11 55 77 90 40 60 99 44 88 66
-> left to right scan: ( search from left to check until number > 44) , Start:22, Pivotal:44
Begin with 22, next scan the list opposite direction from left to right.
comparing each element with 44 and stopping at the first number> 44.
The number is 55. Swap 44 and 55 to obtain the List.
22 33 11 44 77 90 40 60 99 55 88 66
-> Right to Left: Start:55, Pivotal:44
Beginning with 55, Now scan from right to left until number < 44.
That is 40, interchange them 40 and 44.
22 33 11 40 77 90 44 60 99 55 88 66
-> Left to right: Start:40, Pivotal:44
Begin with 40, scan from left to right Until > 44, that 77.
Interchange 44 and 77.
22 33 11 40 44 90 77 60 99 55 88 66
-> Right to Left: Start:77, Pivotal:44
Start with 77, scan right to left until < 44, we don't meet such number.
so 44 is reached to its real position. Now all elements present before 44 are < 44 and after > 44.
so the list is divided into sub list having 44 in it's correct position.
22 33 11 40 44 90 77 60 99 55 88 66
first sublist: 22 33 11 40
second sublist: 90 77 60 99 55 88 66
Before Sort Pivotal 44 Position: 0
After Sort Final Position of Pivotal 44: 4
Like this way we will recursively check other numbers also.
My Analysis:
In Bubble Sort Every Pass (Every Outer loop) , we will filter highest element put it in the last.
In Selection Sort Every Pass (Every Outer loop), we will filter smallest element put in the first.
But in QuickSort For every pivotal pass, we will find and allocate exact location of that pivotal element and leftside < pivotal < rightside
public class Quicksort {
public static void main(String args[]) {
int[] ram = { 20, 30, 10, 9 };
sort(ram, 0, ram.length - 1);
for(int i=0; i < ram.length; i++) {
System.out.print(ram[i]);
System.out.print(" ");
}
}
public static void sort(int[] a, int beg, int end) {
if (beg >= end) {
return;
}
int loc = quick(a, beg, end);
sort(a, beg, loc - 1);
sort(a, loc + 1, end);
}
public static int quick(int[] a, int beg, int end) {
int left = beg;
int right = end;
int loc = beg;
while (left <= right) {
// scan from right to left
while (a[loc] <= a[right] && loc != right) {
right = right - 1;
}
if (loc == right) {
System.out.println("right location-->" + loc);
return loc;
}
if (a[loc] > a[right]) {
int temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
// scan from left to right
while (a[left] <= a[loc] && left != loc) {
left = left + 1;
}
if (loc == left) {
System.out.println("left location-->" + loc);
return loc;
}
if (a[left] > a[loc]) {
int temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
return loc;
}
}
QuickSort:
Divide and Conquer type. Sorting set is divided into sorting 2 smaller sets.
Find final positon for each pivotal and give 2 sublists ( sublist1 < pivotal < sublist2) like this way.
Reduction step of the this algorithm finds the final position of the numbers
means
we are starting with 44 using quicksort technique we will find the final position of 44th in the list
This is simple, we will right to left scan and left to right scan
pivotal number: 44, we are finding final position for this number lets see ?
Current position of 44: 0, after quicksort we will find the correct position for this 44.
Final Position of 44: ?
Ex: Suppose A is the list of 12 numbers.
44 33 11 55 77 90 40 60 99 22 88 66
-> right to left scan: ( search from right to check until number < 44), Start:66, Pivotal:44
First Number 44, last number 66, scan the list from right to left.
comparing each number with 44 and stopping at the number < 44
That is 22. Interchange 44 and 22 to obtain the list
22 33 11 55 77 90 40 60 99 44 88 66
-> left to right scan: ( search from left to check until number > 44) , Start:22, Pivotal:44
Begin with 22, next scan the list opposite direction from left to right.
comparing each element with 44 and stopping at the first number> 44.
The number is 55. Swap 44 and 55 to obtain the List.
22 33 11 44 77 90 40 60 99 55 88 66
-> Right to Left: Start:55, Pivotal:44
Beginning with 55, Now scan from right to left until number < 44.
That is 40, interchange them 40 and 44.
22 33 11 40 77 90 44 60 99 55 88 66
-> Left to right: Start:40, Pivotal:44
Begin with 40, scan from left to right Until > 44, that 77.
Interchange 44 and 77.
22 33 11 40 44 90 77 60 99 55 88 66
-> Right to Left: Start:77, Pivotal:44
Start with 77, scan right to left until < 44, we don't meet such number.
so 44 is reached to its real position. Now all elements present before 44 are < 44 and after > 44.
so the list is divided into sub list having 44 in it's correct position.
22 33 11 40 44 90 77 60 99 55 88 66
first sublist: 22 33 11 40
second sublist: 90 77 60 99 55 88 66
Before Sort Pivotal 44 Position: 0
After Sort Final Position of Pivotal 44: 4
Like this way we will recursively check other numbers also.
My Analysis:
In Bubble Sort Every Pass (Every Outer loop) , we will filter highest element put it in the last.
In Selection Sort Every Pass (Every Outer loop), we will filter smallest element put in the first.
But in QuickSort For every pivotal pass, we will find and allocate exact location of that pivotal element and leftside < pivotal < rightside
public class Quicksort {
public static void main(String args[]) {
int[] ram = { 20, 30, 10, 9 };
sort(ram, 0, ram.length - 1);
for(int i=0; i < ram.length; i++) {
System.out.print(ram[i]);
System.out.print(" ");
}
}
public static void sort(int[] a, int beg, int end) {
if (beg >= end) {
return;
}
int loc = quick(a, beg, end);
sort(a, beg, loc - 1);
sort(a, loc + 1, end);
}
public static int quick(int[] a, int beg, int end) {
int left = beg;
int right = end;
int loc = beg;
while (left <= right) {
// scan from right to left
while (a[loc] <= a[right] && loc != right) {
right = right - 1;
}
if (loc == right) {
System.out.println("right location-->" + loc);
return loc;
}
if (a[loc] > a[right]) {
int temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
// scan from left to right
while (a[left] <= a[loc] && left != loc) {
left = left + 1;
}
if (loc == left) {
System.out.println("left location-->" + loc);
return loc;
}
if (a[left] > a[loc]) {
int temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
return loc;
}
}
No comments:
Post a Comment