Skip to content

Added Quick,Bubble,Shell,Merge Sort #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions Sorting algorithms/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Java implementation of bubble sort
*
* @author Carlos Abraham Hernandez ([email protected])
*/


public class BubbleSort {

void bubbleSort(int arr[]){
for (int i = 0; i < arr.length-1; i++)
for (int j = 0; j < arr.length-i-1; j++)
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}

// Function to print elements
void printArray(int arr[]){
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver method to test above
public static void main(String args[]){

BubbleSort bubble_sort = new BubbleSort();
int arr[] = {46, 24, 33, 10, 2, 81, 50};

System.out.println("Unsorted array:");
bubble_sort.printArray(arr);

System.out.println("Sorted array:");
console.log(bubble_sort.printArray(arr));
}
}
94 changes: 94 additions & 0 deletions Sorting algorithms/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Java implementation of merge sort
*
* @author Carlos Abraham Hernandez
* @email [email protected]
*/

import java.util.Arrays;

public class MergeSort{

// Merge the two half into a sorted data.
public void merge(int arr[], int left, int middle, int right){

int n1 = middle - left + 1;
int n2 = right - middle;

int L[] = new int [n1];
int R[] = new int [n2];

for (int i=0; i<n1; ++i)
L[i] = arr[left + i];
for (int j=0; j<n2; ++j)
R[j] = arr[middle + 1+ j];


int i = 0, j = 0;

int k = left;
while (i < n1 && j < n2){
if (L[i] <= R[j]){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1){
arr[k] = L[i];
i++;
k++;
}

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

// Main function that sorts arr[l..r] using
void sort(int arr[], int left, int right){
if (left < right){
// Find the middle point
int middle = (left+right)/2;

// Sort first and second halves
sort(arr, left, middle);
sort(arr , middle+1, right);

// Merge the sorted halves
merge(arr, left, middle, right);
}
}

//Default values
void sort(int arr[]){
int l = 0; // First index
int r = arr.length-1; // Last index
sort(arr, l, r);
}

// Test Functionality
public static void main(String args[]){

int[] arr = new int[20];

for(int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random()*999 + 1);
}

System.out.println("Unordered List: \n" + Arrays.toString(arr));

MergeSort m = new MergeSort();
m.sort(arr);

System.out.println("Ordered List: \n" + Arrays.toString(arr));
}
}
78 changes: 78 additions & 0 deletions Sorting algorithms/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Java program for implementation of QuickSort
class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++;

// swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// swap arr[i+1] and arr[high] (or pivot)
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;

return i+1;
}


/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr, low, high);

// Recursively sort elements before
// partition and after partition
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

// Driver program
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;

QuickSort ob = new QuickSort();
ob.sort(arr, 0, n-1);

System.out.println("sorted array");
printArray(arr);
}
}
58 changes: 58 additions & 0 deletions Sorting algorithms/ShellSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class ShellSort
{
/* An utility function to print array of size n*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

/* function to sort arr using shellSort */
int sort(int arr[])
{
int n = arr.length;

// Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already
// in gapped order keep adding one more element
// until the entire array is gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap
// sorted save a[i] in temp and make a hole at
// position i
int temp = arr[i];

// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];

// put temp (the original a[i]) in its correct
// location
arr[j] = temp;
}
}
return 0;
}

// Driver method
public static void main(String args[])
{
int arr[] = {12, 34, 54, 2, 3};
System.out.println("Array before sorting");
printArray(arr);

ShellSort ob = new ShellSort();
ob.sort(arr);

System.out.println("Array after sorting");
printArray(arr);
}
}