You need to sign in to do that
Don't have an account?

Help needed on Bubble Sort
Hi,
I am trying to implemet bubble sort, but i am getting error. Below is my code.
public class bubbleSort {
List<Integer> values = new List<Integer>{10,9,8,7,6,5,4,3,2,1};
integer i = 0;
integer iterationsize = values.size();
for(i=0; i < iterationsize; i++)
{
boolean swapped = false;
for(integer j = 0;j < iterationsize; j++)
{
if( i + 1 == values.size()) break;
if(values.get(i) > values.get(i + 1))
{
integer swapVal = values.get(j);
swapped = true;
values.set(j, values.get(j+1));
values.set(j+1,swapVal);
}
System.debug('Iterations List=='JSON.serialize(values));
}
iterationSize--;
System.debug('SWAPPED =='+swapped);
System.debug(JSON.serialize(values));
if(!swapped) {i=values.size();}
}
System.debug('Final List of values=='+ JSON.serialize(values));
}
Error: @ line 7: expecting right curly bracket, found 'for'
Thanks in advance
I am trying to implemet bubble sort, but i am getting error. Below is my code.
public class bubbleSort {
List<Integer> values = new List<Integer>{10,9,8,7,6,5,4,3,2,1};
integer i = 0;
integer iterationsize = values.size();
for(i=0; i < iterationsize; i++)
{
boolean swapped = false;
for(integer j = 0;j < iterationsize; j++)
{
if( i + 1 == values.size()) break;
if(values.get(i) > values.get(i + 1))
{
integer swapVal = values.get(j);
swapped = true;
values.set(j, values.get(j+1));
values.set(j+1,swapVal);
}
System.debug('Iterations List=='JSON.serialize(values));
}
iterationSize--;
System.debug('SWAPPED =='+swapped);
System.debug(JSON.serialize(values));
if(!swapped) {i=values.size();}
}
System.debug('Final List of values=='+ JSON.serialize(values));
}
Error: @ line 7: expecting right curly bracket, found 'for'
Thanks in advance
you missing method in your class Please add method and put code into method and try again and you also missing a '+' in debug log on that line
System.debug('Iterations List=='JSON.serialize(values));
try below code and save it again Thanks
Mark it best answer if it helps you so it make proper solution for others in future
It compiled successfully, but sorting is not done.
Can u have a look.
Thanks
How about below code ..
Thanks,
Optimized bubble sort - if sorting is done before the n-1-i times then no need to iterate till n-1-i times, simply break the loop and display the output.
Below is the Optimized bubble sort. Used flag swapped to find if list has been sorted completelty. When flow does not go inside if condition of inner loop then this means, list has been already sorted and this flag will remain false. Check the flag outsite the inner loop, if it is false then break the loop.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! HOLA !!!!!!!!!!!!!!!!!!!!!!!! The list has been sorted !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class BubbleSorting {
public static void sortList(List<Integer> ListToSort){
system.debug('**** Unsorted List: '+ListToSort);
Integer n = ListToSort.size();
for(Integer i=0; i<n-1; i++){
Boolean swapped = false;
for(Integer j =0; j<n-1-i; j++){
if(ListToSort[j] > ListToSort[j+1]){
system.debug('******ListToSort[j]: '+ListToSort[j]);
system.debug('******ListToSort[j+1]: '+ListToSort[j+1]);
Integer temp = ListToSort[j];
ListToSort.Set(j,ListToSort[j+1]); // Equivalent to ListToSort[j] = ListToSort[j+1];
ListToSort.Set(j+1,temp); // Equivalent to ListToSort[j+1] = temp;
swapped = true;
}
}
//if no further sorting is needed
if(!swapped){
break;
}
}
system.debug('**** Sorted List: '+ListToSort);
}
}
Thanks
************************** Mark it best answer if it helps you. This is my first post in forum ********************************