You need to sign in to do that
Don't have an account?
Sathish Kumar 241
Print Iteration's in Quick Sort
Hi, I have implemented Quick Sort(recursion).
I need to print all the iterations.
Plz guide me.
Below is my code:
public class bubbleSort1 {
// public list<integer> values {get;set;}
public String userInput {get;set;}
public List<Integer> values {GET; SET;}
public set<string> itList {get; set;}
public set<string> itListquick {get; set;}
public list<Integer> getbubbleSortMethod(){
itList = new set<string>();
// values = new list<integer>();
// values.add(5);
// values.add(8);
integer iterationsize = values.size();
integer temp = 0;
integer temp5;
boolean b;
integer count=0;
for(integer i=0; i < iterationsize; i++){
for(integer j=1; j < (iterationsize-i); j++){
temp5=values[j-1];
if(values[j-1] > values[j]){
//swap the elements!
temp = values[j-1];
values[j-1] = values[j];
values[j] = temp;
}
if(values[j-1] != temp5){
b=true;
system.debug('swaped result is'+ values);
}
if(b==false)
count=count+1;
}
system.debug('swaped result is'+ values);
string str = '';
for( integer v :values){
str += string.valueOf(v+',');
}
itList.add(str);
}
System.debug('values ' + values);
system.debug('No of iterations are'+ values.size());
return values;
}
public void display(){
system.debug('User input...' +userInput);
List<String> temp = userInput.split(',');
values = NEW lIST<iNTEGER>();
for(String s : temp){
values.add(Integer.valueOf(s));
}
getbubbleSortMethod();
}
public List<Integer> getvalues {
get {
return values;
}
set;}
public void displayquicksort(){
system.debug('User input...' +userInput);
List<String> temp = userInput.split(',');
values = NEW lIST<iNTEGER>();
for(String s : temp){
values.add(Integer.valueOf(s));
}
sort(values);
}
// private int input[];
// private int length;
public list<integer> lstInteger = new list<Integer>();
integer len;
public void sort(list<Integer> qs){
if (qs == null || qs.size() == 0) {
return;
}
this.lstInteger = qs;
len = qs.size();
quickSort(0, len - 1);
}
/*
* This method implements in-place quicksort algorithm recursively.
*/
private void quickSort(integer low, integer high) {
integer i = low;
integer j = high;
itListquick = new set<string>();
// pivot is middle index
integer pivot = lstInteger[low + (high - low) / 2];
// Divide into two arrays
while (i <= j) {
/**
* As shown in above image, In each iteration, we will identify a
* number from left side which is greater then the pivot value, and
* a number from right side which is less then the pivot value. Once
* search is complete, we can swap both numbers.
*/
while (lstInteger[i] < pivot) {
i++;
}
while (lstInteger[j] > pivot) {
j--;
}
if (i <= j) {
swap(i, j);
// move index to next position on both sides
i++;
j--;
}
}
// calls quickSort() method recursively
if (low < j) {
quickSort(low, j);
}
if (i < high) {
quickSort(i, high);
}
}
private void swap(integer i, integer j) {
integer temp = lstInteger[i];
lstInteger[i] = lstInteger[j];
lstInteger[j] = temp;
}
}
I need to print all the iterations.
Plz guide me.
Below is my code:
public class bubbleSort1 {
// public list<integer> values {get;set;}
public String userInput {get;set;}
public List<Integer> values {GET; SET;}
public set<string> itList {get; set;}
public set<string> itListquick {get; set;}
public list<Integer> getbubbleSortMethod(){
itList = new set<string>();
// values = new list<integer>();
// values.add(5);
// values.add(8);
integer iterationsize = values.size();
integer temp = 0;
integer temp5;
boolean b;
integer count=0;
for(integer i=0; i < iterationsize; i++){
for(integer j=1; j < (iterationsize-i); j++){
temp5=values[j-1];
if(values[j-1] > values[j]){
//swap the elements!
temp = values[j-1];
values[j-1] = values[j];
values[j] = temp;
}
if(values[j-1] != temp5){
b=true;
system.debug('swaped result is'+ values);
}
if(b==false)
count=count+1;
}
system.debug('swaped result is'+ values);
string str = '';
for( integer v :values){
str += string.valueOf(v+',');
}
itList.add(str);
}
System.debug('values ' + values);
system.debug('No of iterations are'+ values.size());
return values;
}
public void display(){
system.debug('User input...' +userInput);
List<String> temp = userInput.split(',');
values = NEW lIST<iNTEGER>();
for(String s : temp){
values.add(Integer.valueOf(s));
}
getbubbleSortMethod();
}
public List<Integer> getvalues {
get {
return values;
}
set;}
public void displayquicksort(){
system.debug('User input...' +userInput);
List<String> temp = userInput.split(',');
values = NEW lIST<iNTEGER>();
for(String s : temp){
values.add(Integer.valueOf(s));
}
sort(values);
}
// private int input[];
// private int length;
public list<integer> lstInteger = new list<Integer>();
integer len;
public void sort(list<Integer> qs){
if (qs == null || qs.size() == 0) {
return;
}
this.lstInteger = qs;
len = qs.size();
quickSort(0, len - 1);
}
/*
* This method implements in-place quicksort algorithm recursively.
*/
private void quickSort(integer low, integer high) {
integer i = low;
integer j = high;
itListquick = new set<string>();
// pivot is middle index
integer pivot = lstInteger[low + (high - low) / 2];
// Divide into two arrays
while (i <= j) {
/**
* As shown in above image, In each iteration, we will identify a
* number from left side which is greater then the pivot value, and
* a number from right side which is less then the pivot value. Once
* search is complete, we can swap both numbers.
*/
while (lstInteger[i] < pivot) {
i++;
}
while (lstInteger[j] > pivot) {
j--;
}
if (i <= j) {
swap(i, j);
// move index to next position on both sides
i++;
j--;
}
}
// calls quickSort() method recursively
if (low < j) {
quickSort(low, j);
}
if (i < high) {
quickSort(i, high);
}
}
private void swap(integer i, integer j) {
integer temp = lstInteger[i];
lstInteger[i] = lstInteger[j];
lstInteger[j] = temp;
}
}