-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
104 lines (88 loc) · 2.65 KB
/
BubbleSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.*;
public class BubbleSort
{
public void Sort(int[] arr) //creating a function with the name of Sort which exactly does what is says
{
int n = arr.length; // getting length of the array
for(int i=0;i<n;i++)
{
boolean swapped = false; // creating a boolean variable to check if the array is sorted or not
for(int j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1]) //checking for the condition for ascending order
{
int temp = arr[j]; //swapping the elements based on the condition
arr[j]=arr[j+1];
arr[j+1]=temp;
swapped = true;
}
}
if(!swapped) //checking if the program hit a break
break;
}
}
public static void main(String[] args) { //creating main method
BubbleSort obj = new BubbleSort();
Scanner xp = new Scanner(System.in);
System.out.println("Enter the size of the array");
int n = xp.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++)
{
arr[i]=xp.nextInt();
}
obj.Sort(arr); //passing the current array into sort function to sort it
System.out.println("The sorted array is");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
xp.close(); //closing the scanner class
}
}
// Optimized java implementation of Bubble sort
// import java.io.*;
// class GFG {
// // An optimized version of Bubble Sort
// static void bubbleSort(int arr[], int n)
// {
// int i, j, temp;
// boolean swapped;
// for (i = 0; i < n - 1; i++) {
// swapped = false;
// for (j = 0; j < n - i - 1; j++) {
// if (arr[j] > arr[j + 1]) {
// // Swap arr[j] and arr[j+1]
// temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// swapped = true;
// }
// }
// // If no two elements were
// // swapped by inner loop, then break
// if (swapped == false)
// break;
// }
// }
// // Function to print an array
// static void printArray(int arr[], int size)
// {
// int i;
// for (i = 0; i < size; i++)
// System.out.print(arr[i] + " ");
// System.out.println();
// }
// // Driver program
// public static void main(String args[])
// {
// int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
// int n = arr.length;
// bubbleSort(arr, n);
// System.out.println("Sorted array: ");
// printArray(arr, n);
// }
// }
// This code is contributed
// by Nikita Tiwari.