forked from yasirunilan/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34839dd
commit d22ad43
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |