0

From what I've read, even though their big Oh notation is the same, when bubble sorting and selection sorting an array, as the size of the array grows, selection sort should outperform bubble sort.

But in my code, bubble sort is consistently outperforming selection sort as the array gets bigger.

In the program, the user specifies the number of items to be in the array, and a number of times that an array of that many items should be created and sorted (to get a more accurate result).

This is my code:

import java.util.Scanner;
import java.util.Random;

public class SelectionSorting
{
  public static int[] arr;
  public static long runningSelectionTime, runningBubbleTime;

   public static void main(String[] args)
   {
     Scanner in = new Scanner(System.in);
     System.out.println("Please enter the number of the items in the array: ");
     int i = in.nextInt();
     System.out.println("Please enter the number of iterations: ");
     int iters = in.nextInt();
     arr = new int[i];
     for (int x = 0; x < iters; x++)
     {
       for (int n = 0; n < i; n++)
       {
         arr[n] = randomness();
        }
       long startSelectionTime = System.nanoTime();
       selectionSort(arr);
       long endSelectionTime = System.nanoTime();
       runningSelectionTime += endSelectionTime - startSelectionTime;
     }
     System.out.println("Selection Sort: ");
     System.out.println("Total running time: " + runningSelectionTime + " and the average is " + runningSelectionTime/iters);

    for (int x = 0; x < iters; x++)
    {
      for (int n = 0; n < i; n++)
      {
        arr[n] = randomness();
      }
      long startBubbleTime = System.nanoTime();
      bubbleSort(arr);
      long endBubbleTime = System.nanoTime();
      runningBubbleTime += endBubbleTime - startBubbleTime;
    }
    System.out.println("Bubble Sort: ");
    System.out.println("Total running time: " + runningBubbleTime + " and the average is " + runningBubbleTime/iters);
   }

   public static void selectionSort(int[] array)
   {
     for (int i = 0; i < array.length - 1; i++)
     {
       int iMin = i;
       for (int j = i + 1; j < array.length; j++)
       {
         if (array[j] < array[iMin]) 
         {
          iMin = j;
         }
       }
       if (iMin != i)
       {
         int temp = array[i];
         array[i] = array[iMin];
         array[iMin] = temp;
       }
     }
  }

   public static void bubbleSort(int[] arr)
   {
     int unsorted = arr.length;
     while (unsorted != 0) 
     {
       int lastSwap = 0;
       for (int i = 1; i < unsorted; i++)
       {
         if (arr[i - 1] > arr[i])
         {
           swap(arr, i, i - 1);
           lastSwap = i;
         }
       }
       unsorted = lastSwap;
     }
   }

   private static void swap(int[] arr, int a, int b)
   {
     int temp = arr[a];
     arr[a] = arr[b];
     arr[b] = temp;
   }

   public static int randomness()
   {
    Random rand = new Random();
    int random = rand.nextInt();
    if (random < 0)
    {
      random = random * -1;
    }
    do {
      random = random / 10;
    } while (random > 100);
    return random;
  }

}

If you try putting in 500 and 1000, the running time of bubble sort will be shorter than that of selection sort.

Interestingly enough, if I get rid of the variable "iters," then the results are as expected. However, it would seem that iters should make the running time more accurate, not less.

Any ideas as to why that is? Did I do something wrong? Is it possible for bubble sort to outperform selection sort (consistently)?

(To prevent any confusion, I have seen the question Why is my Java based Bubble Sort Outperforming my Selection sort and my Insertion Sort?, but it does not address the same problem.)

Community
  • 1
  • 1
  • 2
    Big Oh is different for different cases. Bubble sort is most efficient `Big Oh == n` when data is sorted, and least efficient nsquared when data is in reverse order. – BevynQ Jun 30 '15 at 01:28
  • If you have not done it already, have a look at [this post](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java?lq=1). The problem *could* be in your benchmarking methodology. – PM 77-1 Jun 30 '15 at 01:29
  • 1
    your bubble sort does not work – BevynQ Jun 30 '15 at 01:40
  • @BevynQ I understand that big Oh is different for different cases. However, in this case, bubble sort is consistently faster than selection sort, and the arrays are consistently different (it is not possible that they are all O(n)). I get that big Oh is different for different cases - my question is why bubble sort is outperforming selection sort consistently when theoretically it shouldn't? – Poetical Scientist Jun 30 '15 at 01:41
  • @BevynQ thank you for pointing that out. I copied the wrong bubble sort from my files... Sorry about that. Please see my edited post. – Poetical Scientist Jun 30 '15 at 01:48
  • Why do you have this calculation within the for loop: runningBubbleTime += endBubbleTime - startBubbleTime;? – NoChance Jun 30 '15 at 01:54
  • @EmmadKareem So that it keeps track of total time as it goes through all the iterations. If I put it outside the loop then it will include the time it takes to create the array(s), which would take longer. – Poetical Scientist Jun 30 '15 at 01:59
  • You keep a bubble sort that doesn't work in your files? Why? – user207421 Jun 30 '15 at 02:22
  • @EJP I had multiple bubble sorts from various sources open in my compiler... Happened to copy the wrong one. Needless to say, it is now deleted from my computer. – Poetical Scientist Jun 30 '15 at 02:28

1 Answers1

1

Your bubble sort is faster because it does not work.

      arr[i] = temp;

should be

      arr[i-1] = temp;

output for 10000 * 3

Selection Sort:
Total running time: 216077472 and the average is 72025824
Bubble Sort:
Total running time: 402583050 and the average is 134194350

output for 500 * 1000

Selection Sort:
Total running time: 123621068 and the average is 123621
Bubble Sort:
Total running time: 317450183 and the average is 317450

using using jdk 7

BevynQ
  • 8,089
  • 4
  • 25
  • 37
  • Ah... that would account for a lot... Thank you! I did change that in my code, but I'm still getting a shorter running time for bubble sort than selection sort. Is it the same for you? – Poetical Scientist Jun 30 '15 at 01:51
  • So sorry to ask yet another question... But is it the same when you enter 500 and 1000? Up until then it seems like selection sort is better, but once I use those numbers bubble sort outperforms yet again... – Poetical Scientist Jun 30 '15 at 01:58
  • I guess I better try another machine... Thanks so much for all your help! – Poetical Scientist Jun 30 '15 at 02:03