top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

My question about comparing run time performance of bubble sort and selection sort?

+3 votes
509 views

1) Implement both algorithms
2) Test both on three cases of data:
a) sorted in increase order,
b) sorted in decrease order,
c) randomly
3) Use data of different sizes: 10000, 100000, 1000000.

posted Mar 5, 2014 by As M Ob

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Testing can not be done on the site, people can help you in code only, rest you need to do yourself.
Okey . Can help me to write the code . !!

1 Answer

+1 vote

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);
        }
    }
}
answer Mar 6, 2014 by Salil Agrawal
Similar Questions
+1 vote

1- Implement the counting sort algorithm using C++
2- use data of different size in your code 10000,100000,1000000

+2 votes

Can someone share the C code for selection or bubble sort for string. String comparison should be based on dictionary comparison.

+1 vote

if input this Data size 10000,100000,1000000

...