Skip to content

added bubble,selection,merge and quick sort #18

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
30 changes: 30 additions & 0 deletions Sorting algorithms/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swapping temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}

public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = {64, 34, 25, 12, 22, 11, 90};
ob.bubbleSort(arr);

//displaying the array
System.out.println("The sorted array:");
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
95 changes: 95 additions & 0 deletions Sorting algorithms/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], 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] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];


/* Merge the temp arrays */

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

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

/* Copy remaining elements of L[] if any */
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++;
}
}

// function that sorts arr[l..r]
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Finding middle point
int m = (l+r)/2;

// Sorting first half
sort(arr, l, m);
//Sorting second half
sort(arr , m+1, r);

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


public static void main(String args[])
{
int arr[] = {5, 7, 2, 10, 17, 4, 3};


MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);

System.out.println("\nSorted array");
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
61 changes: 61 additions & 0 deletions Sorting algorithms/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class QuickSort
{
/* Assumes a pivot element and places it correctly,
consequently places all elements smaller than pivot to it's left
and bigger elements to it's right */
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()

void sort(int arr[], int low, int high)
{
if (low < high)
{
// pi is partitioning index
int pi = partition(arr, low, high);

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

public static void main(String args[])
{
int arr[] = {5, 7, 2, 10, 17, 4, 3};
int n = arr.length;

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

System.out.println("sorted array");
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println(); }
}
37 changes: 37 additions & 0 deletions Sorting algorithms/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;

// moving boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Finding the minimum element
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swapping minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {5, 7, 2, 10, 17, 4, 3};
ob.sort(arr);
System.out.println("The sorted array");

//displaying the array
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();

}
}