function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sathish Kumar 241Sathish Kumar 241 

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
sfdcMonkey.comsfdcMonkey.com
hi kumar
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
public class bubbleSort {

    public static void bubbleSortMethod(){
    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));
        }
}
Thanks
Mark it best answer if it helps you so it make proper solution for others in future

 
Sathish Kumar 241Sathish Kumar 241
Hi piyush_soni,

It compiled successfully, but sorting is not done.
Can u have a look.
Thanks
 
ra1ra1
Hi Sathish,

How about below code ..
 
public class BubbleSort {

    public static void bubbleSortMethod(){
        List<Integer> values = new List<Integer>{10,9,8,7,6,5,4,3,2,1};

        integer iterationsize = values.size();
        integer temp = 0;
        
        for(integer i=0; i < iterationsize; i++){
            for(integer j=1; j < (iterationsize-i); j++){
                
                if(values[j-1] > values[j]){
                    //swap the elements!
                    temp = values[j-1];
                    values[j-1] = values[j];
                    values[j] = temp;
                }
            }
        }
        
        System.debug('values ' + values);
    }
}

Thanks,
 
krishna kumar mahtokrishna kumar mahto
Normal Bubble sort - Inneer loop will iterate over n-1-i times to sort the numbers ( n = lenght of the list)
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 ********************************