Skip to content

Commit

Permalink
Implemented BubbleSort.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Anandra-Singh authored Oct 5, 2017
1 parent 34839dd commit d22ad43
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Bubble-Sort/Bubble_Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// run javac BubbleSort.java to Compile.
// run java BubbleSort to run Program.
import java.util.Scanner;
/**
Bubble sort algorithm
*/
public class BubbleSort
{
private int[] intData;
/**
Constructor
@param inOutArray the array to be sorted
*/
public BubbleSort(int[] inOutArray)
{
intData = inOutArray;
}
/**
Sorts the attribute inData
*/
public void sort()
{
for (int i = intData.length - 1; i > 0; i--)
{
placeMax(i);
}
}
/**
Bubble the largest value
@param end location where unprocessed array ends
*/
private void placeMax(int end)
{
for (int i = 0; i < end; i++)
if (intData[i] > intData[i+1])
swap(i, i+1);
}
/**
interchange values between two locations of the array
@param first one of the location to be interchanged
@param second the other location to be interchanged
*/
private void swap(int first, int second)
{
int temp = intData[first];
intData[first] = intData[second];
intData[second] = temp;
}
}

0 comments on commit d22ad43

Please sign in to comment.