'BubbleSort algorithm using heuristics
I'm supposed to the output passes: 102, swaps: 8979, compares: 14127 but I get the output of passes: 102, swaps: 8979, compares: 19278 I'm having more comparisons than needed. Any way I can lower them? I believe I'm double counting. I'm checking the compares counter and it seems to be in too many places. I have no idea how to fix this issue.
public int[] sorth(int[] data) {
/* your code goes here */
boolean swapped;
int i, n = data.length;
do {
swapped = false;
ctrs.count(passCounter);
for (i = 0; i <= n - 2; i++) {
if (data[i] > data[i + 1]) {
swap(data, i, i + 1);
swapped = true;
}
ctrs.count(cmpCounter);
}
if (!swapped) {
break;
}
swapped = false;
ctrs.count(passCounter);
for (i = n - 2; i >= 0; i--) {
if (data[i] > data[i + 1]) {
swap(data, i, i + 1);
swapped = true;
}
ctrs.count(cmpCounter);
}
}
while (swapped);
return data;
}
Solution 1:[1]
see the bubble sort funtion where for an array of length len we are iterating len-1 in outer for loop and inner for loop we are iterating over the elements in the arrays excepting the last elements because they are already in correct position.
import java.util.Arrays;
class MainClass{
static int passCounter = 0, cmpCounter = 0;
public static int[] sorth(int[] data) {
/* your code goes here */
passCounter = 0;
cmpCounter = 0;
boolean swapped;
int i, n = data.length;
do {
swapped = false;
//ctrs.count(passCounter);
passCounter++;
for (i = 0; i <= n - 2; i++) {
if (data[i] > data[i + 1]) {
//swap(data, i, i + 1);
var temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
swapped = true;
}
//ctrs.count(cmpCounter);
cmpCounter++;
}
if (!swapped) {
break;
}
swapped = false;
//ctrs.count(passCounter);
passCounter++;
for (i = n - 2; i >= 0; i--) {
if (data[i] > data[i + 1]) {
//swap(data, i, i + 1);
var temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
swapped = true;
}
//ctrs.count(cmpCounter);
cmpCounter++;
}
}
while (swapped);
return data;
}
static int cmp = 0;
//general bubble sort
static void bubbleSort(int [] arr){
cmp =0;
//iterating len -1 times
for ( int i=0;i<arr.length-1;i++){
boolean exe = false;
//j<arr.length-i-1 (here -1 one because we want to make sure there are enough elements and -i because we want to avoid iterating over the last elements since they are in correct order)
for(int j=0;j<arr.length-i-1;j++){
if(arr[j] > arr[j+1]){
cmp++;//comparision
exe=true;
//using xor to swap
arr[j]^=arr[j+1];
arr[j+1]^=arr[j];
arr[j]^=arr[j+1];
}
}
if(!exe)
break;
}
}
public static void main(String ... $){
var out = System.out;
int [] data = new int[]{323355, 343, 233, 52 ,2};
int [] data1 = new int[]{323355, 343, 233, 52 ,2};
//sorting
sorth(data);
for(int v : data)
out.print(v+" ");
out.println();
out.println("comparisions : "+cmpCounter);
bubbleSort(data1);
for(int v : data1)
out.print(v+" ");
out.println();
out.println("comparisions : "+cmp);
}
}
Output:
javac MainClass.java && java MainClass
2 52 233 343 323355
comparisions : 20
2 52 233 343 323355
comparisions : 10
For more information regarding bullble sort plese visit GFG site.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
