Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 134 additions & 10 deletions src/main/java/sortalgorithms/Sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ public class Sort {
private int[] inputArray;


public int[] bubbleSort(int[] inputArray){
public int[] bubbleSort(int[] inputArray) {

int size=inputArray.length-1;
int temp=-1;
int size = inputArray.length - 1;
int temp = -1;

if(size==1)
if (size == 1)
return inputArray;

for(int i=0;i<=size;i++){
for (int i = 0; i <= size; i++) {

for(int j=0;j<size-i;j++){
for (int j = 0; j < size - i; j++) {

if(inputArray[j]>inputArray[j+1]){
if (inputArray[j] > inputArray[j + 1]) {

temp=inputArray[j+1];
inputArray[j+1]=inputArray[j];
inputArray[j]=temp;
temp = inputArray[j + 1];
inputArray[j + 1] = inputArray[j];
inputArray[j] = temp;


}
Expand All @@ -35,6 +35,128 @@ public int[] bubbleSort(int[] inputArray){

}


public int[] selectionSort(int[] inputArray) {

int size = inputArray.length - 1;

if (size == 1)
return inputArray;

for (int i = 0; i < size; i++) {
int index = i;
for (int j = i + 1; j < size; j++) {
if (inputArray[j] < inputArray[index]) {
index = j;//searching for lowest index
}
}
int smallerNumber = inputArray[index];
inputArray[index] = inputArray[i];
inputArray[i] = smallerNumber;
}
return inputArray;

}

public int[] insertionSort(int[] inputArray) {

int size = inputArray.length - 1;
if (size == 1)
return inputArray;
for (int i = 1; i < size; ++i) {
int key = inputArray[i];
int j = i - 1;


while (j >= 0 && inputArray[j] > key) {
inputArray[j + 1] = inputArray[j];
j = j - 1;
}
inputArray[j + 1] = key;
}
return inputArray;

}




// Main function that sorts inputArray[l..r] using
// merge()
public int[] mergeSort(int[] inputArray, int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;

// Sort first and second halves
mergeSort(inputArray, l, m);
mergeSort(inputArray, m + 1, r);

// Merge the sorted halves
merge(inputArray, l, m, r);
}
return inputArray;
}


/* Java program for Merge Sort */
// Merges two subarrays of inputArray[];
// First subarray is inputArray[l..m]
// Second subarray is inputArray[m+1..r]

public void merge(int[] inputArray, int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;

/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];

/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = inputArray[l + i];
for (int j = 0; j < n2; ++j)
R[j] = inputArray[m + 1 + j];


/* Merge the temp arrays */

// Initial indexes of first and second subarrays
int i = 0, j = 0;

// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
inputArray[k] = L[i];
i++;
} else {
inputArray[k] = R[j];
j++;
}
k++;
}

/* Copy remaining elements of L[] if any */
while (i < n1) {
inputArray[k] = L[i];
i++;
k++;
}

/* Copy remaining elements of R[] if any */
while (j < n2) {
inputArray[k] = R[j];
j++;
k++;
}

}




public Sort(int[] inputArray) {
this.inputArray = inputArray;
}
Expand All @@ -46,4 +168,6 @@ public int[] getInputArray() {
public void setInputArray(int[] inputArray) {
this.inputArray = inputArray;
}


}
4 changes: 3 additions & 1 deletion src/main/java/sortalgorithms/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public static void main(String[] args) {

Sort doSort=new Sort(inputs);
System.out.println(Arrays.toString(doSort.bubbleSort(inputs)));

System.out.println(Arrays.toString(doSort.selectionSort(inputs)));
System.out.println(Arrays.toString(doSort.mergeSort(inputs)));
System.out.println(Arrays.toString(doSort.insertionSort(inputs)));



Expand Down