To Implement Three Different Sorting Algorithms: Selection Sort, Quicksort and Bubblesort
To Implement Three Different Sorting Algorithms: Selection Sort, Quicksort and Bubblesort
Program Code:
swap(data,scan,scan+1);
// Selection sort.
int min;
min = index;
min = scan;
}
}
swap(data,min,index);
// Quicksort.
quickSort(array,0,array.length-1);
quickSort(array, pi + 1, end);
// Partition part of an array, and return the index where the pivot
// ended up.
swap(array, i, j);
swap(array,i + 1, end);
return (i + 1);
array[i] = array[j];
array[j] = temp;
System.out.print(array[i]+" ");
System.out.println();
}
Bench.java Output:
Performance Recommendations:
1. For fully sorted data, Bubble Sort performed significantly better than Quicksort and Selection
Sort in all sizes of arrays.
2. For 95% sorted and randomly arranged data, Bubble Sort is much faster than the other two for
smaller data. But Quicksort performed significantly better than the other two for larger array
sizes (100 and greater).
Based on these observations, I recommend Bubble Sort for small data sets and Quicksort for larger data
sets given that the data is random and not already sorted (in which case we wouldn’t require a sorting
algorithm in the first place).
2. How does the type of test data affect which algorithm you should choose?
For fully sorted data, bubble sort would be a better choice over quick sort and selection sort.
For random data, quick sort would be the best choice.
3. Which should you choose if you don’t know anything about the test data?
If the type and size of the test data is unknown, I would choose quick sort since it has the best
time complexity out of the three.
4. Are there circumstances when you should definitely avoid a certain algorithm?
Yes, for circumstances where the data is fully sorted and of large size (over 3000 as per test
results), quick sort must be definitely avoided.