First off excuse that this isn't C++, but its super simple that i think anyone can help me ...just pretend its pseudocode
My question is, which is better? (and of course, is the 2nd code posted even considered bubble sort)
THANKS!
Heres standard bubble sort: It just goes through and makes the unsorted portion smaller each pass.
1rst code:
Code:
public static void bubbleSort(int[] arr) {
for(int i = arr.length - 1; i >= 1; i--){
for(int j = 0; j < i; j++)
{
if(arr[j] > arr[j+1]) {
//swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
2nd code: (it keeps the unsorted portion the same size every pass but it stops 1 pass after it is sorted, while the first code continues till its done)
Code:
public static void sort(int[] arr)
{
int temp;
boolean ordered=false;
while(!ordered)
{
ordered=true;
for(int i=0;i<arr.length-1;++i)
{
if(arr[i]>arr[i+1])
{
ordered=false;
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
}
THANKS AGAIN!
(I was also thinking that it depends on the dataset you receive... is there any why to "measure" how "bad" a dataset is?)