Providing the algo/code for the Selection sort and Bubble sort. The code is for increasing order, you can convert the same to decreasing order by reverse the comparison sign (your homework, we don't solve the homework question) and obviously you need to test for 10k, 100k, 1000k size of data and fix the bug if any (again your homework).
Selection Sort:
Find the smallest element, and put it to the first position.
Find the next smallest element, and put it to the second position.
Repeat until all elements are in the right positions.
public static void selectionSort(int[] arr)
{
// find the smallest element starting from position i
for (int i = 0; i < arr.length - 1; i++)
{
int min = i; // record the position of the smallest
for (int j = i + 1; j < arr.length; j++)
{
// update min when finding a smaller element
if (arr[j] < arr[min])
min = j;
}
// put the smallest element at position i
swap(arr, i, min);
}
}
public static void swap (int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Bubble Sort
Scan the array, swapping adjacent pair of elements if they are not in relative order. This bubbles up the largest element to the end.
Scan the array again, bubbling up the second largest element.
Repeat until all elements are in order.
public static void bubbleSort (int[] data)
{
for (int i = data.length - 1; i >= 0; i--)
{
// bubble up
for (int j = 0; j <= i - 1; j++)
{
if (data[j] > data[j + 1])
swap(data, j, j + 1);
}
}
}