+ Start a Discussion
Sathish Kumar 241Sathish Kumar 241 

Hi help needed,

I need a apex class that illustrates bubble sort. Thanks in advance.
Rowallim TechnologyRowallim Technology
Hi Sathish
I like your BubbleSort method. However, it uses a lot of processing time. Let's take a look:

Code:
<<code for sort, as above>>
string[] s = new string[21];
s[0]='zieafa';
s[1]='ifeapi';
s[2]='uzpaea';
s[3]='azeaca';
s[4]='japwaa';
s[5]='qiufae';
s[6]='lpaife';
s[7]='ziefa';
s[8]='ifepi';
s[9]='uzpea';
s[10]='azeca';
s[11]='japaa';
s[12]='qiuae';
s[13]='lpafe';
s[14]='iefa';
s[15]='fepi';
s[16]='zpea';
s[17]='zeca';
s[18]='apaa';
s[19]='iuae';
s[20]='pafe';
BubbleSort(s);

When you run using this example data, you end up with the following runtime results:
Results:
INFO - Cumulative resource usage:
INFO - Number of SOQL queries: 0 out of 100
INFO - Number of query rows: 0 out of 10000
INFO - Number of DML statements: 0 out of 100
INFO - Number of DML rows: 0 out of 10000
INFO - Number of transaction control statements: 0 out of 20
INFO - Number of script statements: 747
INFO - Maximum heap size: 0 out of 1000000
 

This is a lot of code coverage for a 21-indice array. I sought to bring that number down so it can be used with larger arrays:

Code:
public void qsort(string[] inarray)
{ for(integer i1 = 0; i1 < inarray.size(); i1++)
{ // check for alphabetical
while(i1 < inarray.size()-1 && inarray[i1] < inarray[i1+1])
{ i1++;
}
for(integer i2 = i1; i2 < inarray.size(); i2++)
{ if(inarray[i1] > inarray[i2])
{ swap(inarray, i1, i2);
}
}
}
}
private void swap(String[] inArray, Integer i1, Integer i2)
{ String tmp;
tmp = inarray[i1];
inarray[i1] = inArray[i2];
inArray[i2] = tmp;
}
 
This code checks each indice and locates the lowest value, then swaps it out over each pass, instead of moving it one indice down. However, it sometimes performs unnecessary swaps. The overall results of my sample were still fairly impressive. 

Results:
INFO - Cumulative resource usage:
INFO - Number of SOQL queries: 0 out of 100
INFO - Number of query rows: 0 out of 10000
INFO - Number of DML statements: 0 out of 100
INFO - Number of DML rows: 0 out of 10000
INFO - Number of transaction control statements: 0 out of 20
INFO - Number of script statements: 438 INFO - Maximum heap size: 0 out of 1000000
 

As you can see, from the original 747 lines, it's now down to 438 lines. That's 58% of the original footprint, which is impressive. However, it's still very inefficient. Imagine running a list of 1000 items-- it would run to be extremely large, and might not complete successfully. So, I optimized a bit more and came up with the following code.

Code:
public void dsort(string[] inarray)
{ integer y = 0, z = inarray.size()-1;
while(y < z)
{ integer min = y;
integer max = z;
for(integer i = y; i <= z; i++)
{ if(inarray[i] < inarray[min])
{ min = i;
}
if(inarray[i] > inarray[max])
{ max = i;
}
}
triswap(inarray, y, z, min, max);
y++; z--;
}
}
private void triswap(string[] inarray, integer low, integer high, integer min, integer max)
{ string tmp = inarray[high];
inarray[high] = inarray[max];
inarray[max] = inarray[low];
inarray[low] = inarray[min];
inarray[min] = tmp;
}
 

I looked at it for a while and made sure it worked correctly, since it looks almost too good to be true. Then, I ran the code and tested it, and came up with some amazing results. After getting these results, I even had to doublecheck to see if the sort was finishing correctly. Here's why...

Results:
INFO - Cumulative resource usage:
INFO - Number of SOQL queries: 0 out of 100
INFO - Number of query rows: 0 out of 10000
INFO - Number of DML statements: 0 out of 100
INFO - Number of DML rows: 0 out of 10000
INFO - Number of transaction control statements: 0 out of 20
INFO - Number of script statements: 167
INFO - Maximum heap size: 0 out of 1000000
 

Amazing! I just went from 438 lines to only 167 lines, just by doubling up one loop... So, for the record, that translates to 22% of the original posted code, and 38% of my first attempt at a sorting algorithm. I hope someone finds this algorithm interesting and even useful. This algorithm does have dependencies on the order of the values (most algorithms do); even in a worst case scenario, it still uses less cycles than a brute-force method such as bubble sorting.

Hope it will help you.
please mark it best answer if it helps.
​Thanks
Sathish Kumar 241Sathish Kumar 241
Hi thank u for the code.
But, my requirement is the bubble sort should be implemented on array of integers. Values should be accepted from vf page and displayed the sorted order on vf page. 
Thanks in advance.