0% found this document useful (0 votes)
3 views

bubblesort random

The document contains a Java program that implements the Bubble Sort algorithm to sort an array of random integers. It generates an array of 20 random integers, sorts it using Bubble Sort, and counts the number of comparisons made during the sorting process. The program runs this sorting operation 10 times and calculates the average number of comparisons across all runs.

Uploaded by

Suman Chatterjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

bubblesort random

The document contains a Java program that implements the Bubble Sort algorithm to sort an array of random integers. It generates an array of 20 random integers, sorts it using Bubble Sort, and counts the number of comparisons made during the sorting process. The program runs this sorting operation 10 times and calculates the average number of comparisons across all runs.

Uploaded by

Suman Chatterjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Random;
public class BubbleSortExample {
static int bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
int comp=0;
for(int i=0; i < n; i++){

for(int j=1; j < (n-i); j++){


comp++;
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
return comp;
}
public static void main(String[] args) {
int c=0;
double avg;
int[] arr = new int[20];
int[] arrb = new int[20];
Random rd = new Random();
for(int j=1; j <= 10; j++){
for (int i = 0; i <arr.length; i++) {

arr[i] = rd.nextInt(125-1)+1;
}
// int arr[] ={3,60,35,2,45,320,5};
for (int i = 0; i <arr.length; i++) {

arrb[i] = arr[i];
}
System.out.println("Array Before Bubble Sort");
for(int i=0; i < arrb.length; i++){
System.out.print(arrb[i] + " ");
}
System.out.println();

c=bubbleSort(arrb);//sorting array elements using bubble sort

System.out.println("Array After Bubble Sort");


for(int i=0; i < arrb.length; i++){
System.out.print(arrb[i] + " ");
}
System.out.println("No of comparison in bubble sort"+c);

}
avg=c/10;
System.out.println("No of comparison in bubble sort"+avg);

}
}

You might also like