Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/inforkgodara/bubble-sort
A single class implementation of bubble sort
https://github.com/inforkgodara/bubble-sort
bubble-sort data-structures inforkgodara java
Last synced: about 1 month ago
JSON representation
A single class implementation of bubble sort
- Host: GitHub
- URL: https://github.com/inforkgodara/bubble-sort
- Owner: inforkgodara
- Created: 2020-01-12T16:58:43.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-02T11:22:22.000Z (over 4 years ago)
- Last Synced: 2024-10-30T08:21:18.115Z (3 months ago)
- Topics: bubble-sort, data-structures, inforkgodara, java
- Language: Java
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bubble Sort
A simple single class implementation of bubble sort in Java. The user will be prompted to input the number of elements in a list which they want to enter. Sort method works by repeatedly swapping the adjacent elements if they are in the wrong order.
## How to use it.
* Open a command prompt window and go to the directory where you saved the java program (BubbleSort.java).
* Type 'javac BubbleSort.java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line.
* Now, type 'java BubbleSort' to run your program.
* You will be able to see an output line and asking input to enter the number of elements (integer) you want to enter.
* Next you will enter the number of elements it can be space-separated or in a new line.
* After completion of entering elements in list, the program will show sorted list.## Code
```
import java.util.Scanner;/**
* This is an implementation of the bubble sort algorithm.
* @author @inforkgodara
*
*/
public class BubbleSort {/**
* Bubble sort implementation.
* @param list the array to be sorted.
* @param length the integer which size of list.
*/
static void sort(int list[], int length) {
for (int i = 0; i < length-1; i++) {
for (int j = 0; j < length-i-1; j++) {
if (list[j] > list[j+1])
{
int temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
}public static void main(String[] args) {
System.out.print("Enter length of list : ");
Scanner scanner = new Scanner(System.in);int length = scanner.nextInt();
int[] list = new int[length];for (int index = 0; index < length; index++) {
list[index] = scanner.nextInt();
}sort(list, length);
System.out.print("Sorted list: ");
for (int element: list) {
System.out.print(element + " ");
}
}
}
```