Comparisons and Swapa in Bubble-sort Java

Aartiyadav

Neues Mitglied
Hello Everyone, I have a java program where the user can select 1000, 10000, or 100000 elements and I want to be able to count the swaps and comparisons for each of the 3 options of elements above this is my code for the bubble sort array. Can anyone suggest me, where I am wrong?

Java:
public static void bubbleSort(int[] arrayToSort)
{
  int comps=0;
  int swaps=0;


  for(int i = 0; i < arrayToSort.length-1; i++)
  {
     for(int j = 0; j < arrayToSort.length-1; j++)
     {
        comps++;
        if(arrayToSort[j] > arrayToSort[j+1])
        {
           swaps++;
           int temp = arrayToSort[j];
           arrayToSort[j] = arrayToSort[j+1];
           arrayToSort[j+1] = temp; 
        }   
     }   

  }

     System.out.println("\nComparisions: " + comps + "swaps: " + swaps);
}

When I run this code on the java compiler here, the output for every array size is

Comparisons: 99980001 swaps: 0

Comparisons: 99980001 swaps: 0

Edit: I have a tester class that holds a random array with values from 1-100

This is my tester class:
Java:
import java.util.*;
import java.util.Scanner;
import java.util.Random;

public class Tester extends algorithmTest
{
 public static void main(String [] args)
 {
  int options;
  int sortType=0;
  int numElements = 0;
  int algorithm;
  String algName=""; 

  Scanner keyIn = new Scanner(System.in);

  System.out.print("****************MENU****************");
  System.out.print("\nPlease Select The Number Of Elements In The Array:");
  System.out.print("\n1. 1000");
  System.out.print("\n2. 10000");
  System.out.print("\n3. 100000");
  System.out.print("\n4. Quit\n");
  numElements = keyIn.nextInt();

  switch(numElements){
     case 1 :
        numElements = 1000;
        System.out.print("You have selected " + numElements + " elements");         
        break;
     case 2 :
        numElements = 10000;
        System.out.print("You have selected " + numElements + " elements");     
        break;
     case 3 :
        numElements = 100000;
        System.out.print("You have selected " + numElements + " elements"); 
        break;
     default :
        System.out.println("Error");
  }     

  System.out.print("\n***********MENU***********");
  System.out.print("\nPlease Select A Sorting Algorithm");
  System.out.print("\n1. Bubble Sort");
  System.out.print("\n2. Enhanced Bubble Sort");
  System.out.print("\n3. Selection Sort");
  System.out.print("\n4. Insertion Sort");
  System.out.print("\n5. Quit\n");
  algorithm = keyIn.nextInt();


  int [] array = new int[numElements];
  Assignment.bubbleSort(array);



  if(algorithm ==1)
  {
     algName = "Bubble Sort: ";
     Assignment.bubbleSort(array);


  }

  else if(algorithm ==2)
  {
     algName = "Enhanced Bubble Sort: ";
     Assignment.bubbleSortEnhanced(array);


  } 

  else if(algorithm ==3)
  {
     algName = "Selection Sort: ";
     Assignment.selectionSort(array);


  }

  else if(algorithm ==4)
  {
     algName = "Insertion Sort: ";
     Assignment.insertionSort(array);


  }



  System.out.print("\n*************MENU****************");
  System.out.print("\nPlease Select A Sorting Option");
  System.out.print("\n1. Random Sorted Array");
  System.out.print("\n2. Sorted Array");
  System.out.print("\n3. Inversely Sorted Array\n");
  sortType=keyIn.nextInt();

     System.out.print("\nRandom Array: ");
     for(int i=0; i<array.length; i++)
     {
     array[i] = (int)(Math.random() * 101);
     System.out.print(array[i] +" ");
     }

     System.out.print("\nSorted Array: ");
     Assignment.bubbleSort(array);
     for(int i=0; i<array.length; i++)
     {
        System.out.print(array[i] +" ");
     }

  if(sortType ==1)
  {
     Assignment.bubbleSort(array);
     System.out.print("\nRandomly Sorted Array: ");
     for(int i=0; i<array.length; i++)
     {
        array[i] = (int)(Math.random() * 101);
        System.out.print(array[i] +" ");

     }

  }

  else if(sortType ==2)
  {
     Assignment.bubbleSort(array);
     System.out.print("\n" +algName);
     for(int i=0; i<array.length; i++)
     {

        System.out.print(array[i] +" ");
     }

  }


  else if(sortType ==3)
  {
     long start = System.nanoTime();
     Assignment. inverseSort(array);
     System.out.print("\n" +algName);
     for(int i=0; i<array.length; i++)
     {

        System.out.print(array[i] +" ");
     }
  long end = System.nanoTime();
     System.out.print("\nThe algorithm was completed in " + (end-start) + " nanoseconds"); 

  } 
  }}
 

KonradN

Top Contributor
irst of all: This is a german forum.

An arrays is a reference, so when you pass an array to a method and change the array in there, the array itself ist changed. So after the first call that you have in line 53 in your test class, the array is sorted.

If you want to compare different sort methods, you should pass a copy of the array instead, e.g. use array.clone() as parameter instead of array.
 

temi

Top Contributor
The array in line 52 is a new Int-Array, so all values are initialized to 0. That leads to no swaps, when you first call the sort algorithm in line 53.

Java:
    public static void main(String[] args) {
        int[] arr= {1,7,13,-5,2,27,11,4,51,42};
        bubbleSort(arr);
    }

Result: Comparisions: 81 swaps: 12
 
Zuletzt bearbeitet:

temi

Top Contributor
If you want to compare different sort methods, you should pass a copy of the array instead, e.g. use array.clone() as parameter instead of array.
Hint: This will only work, when your sorting method is returning the sorted array as a result. Otherwise the passed copy of the array will be sorted inside the method and destroyed afterwards (since it is a locale variable).
 

temi

Top Contributor
The last part of your program is a little bit strange, see the comments:
Java:
    // in this part you are filling the array with random numbers
    System.out.print("\nRandom Array: ");
     for(int i=0; i<array.length; i++)
     {
     array[i] = (int)(Math.random() * 101);
     System.out.print(array[i] +" ");
     }

    // and here the randomly filled array will be instantly sorted
     System.out.print("\nSorted Array: ");
     Assignment.bubbleSort(array);
     for(int i=0; i<array.length; i++)
     {
        System.out.print(array[i] +" ");
     }

    // and here you are trying to sort the perviously sorted array again
  if(sortType ==1)
  {
     Assignment.bubbleSort(array);
     System.out.print("\nRandomly Sorted Array: ");
      // and filling it after sorting with random numbers
     for(int i=0; i<array.length; i++)
     {
        array[i] = (int)(Math.random() * 101);
        System.out.print(array[i] +" ");

     }

  }
 
Zuletzt bearbeitet:

nelsonzop

Neues Mitglied
Bubble Sort beginnt mit den ersten beiden Elementen und vergleicht sie, um zu prüfen, welches größer ist. ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Hier vergleicht der Algorithmus die ersten beiden Elemente und tauscht seit 5 > 1. ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Da diese Elemente bereits in Ordnung sind (8 > 5), tauscht der Algorithmus sie nicht aus
 

nelsonzop

Neues Mitglied
Bubble Sort beginnt mit den ersten beiden Elementen und vergleicht sie, um zu prüfen, welches größer ist. ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Hier vergleicht der Algorithmus die ersten beiden Elemente und tauscht seit 5 > 1. ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Da diese Elemente bereits in Ordnung sind (8 > 5), tauscht der Algorithmus sie nicht aus
hoffe es hilft dir beim verstehen techzpod download mobdro
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Bubble Sort Allgemeine Java-Themen 3
J Array-List Bubble-Sort Allgemeine Java-Themen 12

Ähnliche Java Themen

Neue Themen


Oben