Skip to content

adding bubble sort, quick sort and selection sort #7

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
42 changes: 42 additions & 0 deletions Sorting algorithms/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.Scanner;
/**
*
* @author BrundaSreedhar
*/
public class BubbleSort {
void bubbleSort(int array[])
{
int n = array.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (array[j] > array[j+1])
{
// swap temp and array[i]
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
Scanner sc= new Scanner(System.in);

System.out.println("Enter number of elements to be sorted: ");
int num=sc.nextInt();
int array[] = new int[num];

System.out.println("Enter array elements");
for(int i=0; i< num; i++){
array[i] = sc.nextInt();
}

ob.bubbleSort(array);
System.out.println("Array after bubble sort..");

for(int i=0;i<num;i++){
System.out.print(array[i] + " ");
}
}

}
80 changes: 80 additions & 0 deletions Sorting algorithms/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

import java.util.Scanner;

public 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 */
private 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;
}

/**
*
* @param arr
* @param low
* @param high
*/

void quickSort(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
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}

public static void main(String args[])
{
Scanner sc= new Scanner(System.in);

System.out.println("Enter number of elements to be sorted: ");
int num=sc.nextInt();
int array[] = new int[num];

System.out.println("Enter array elements");
for(int i=0; i< num; i++){
array[i] = sc.nextInt();
}
QuickSort ob = new QuickSort();
ob.quickSort(array, 0, num-1);

for(int i=0;i<num;i++){
System.out.print(array[i] + " ");
}
}
}
47 changes: 47 additions & 0 deletions Sorting algorithms/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

import java.util.Scanner;

public class SelectionSort
{
void selectionSort(int arr[])
{
int n = arr.length;

// Move the boundary of unsorted subarray one by one
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min])
min = j;

// Swap the found minimum element with the first
// element
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
Scanner sc= new Scanner(System.in);

System.out.println("Enter number of elements to be sorted: ");
int num=sc.nextInt();
int array[] = new int[num];

System.out.println("Enter array elements");
for(int i=0; i< num; i++){
array[i] = sc.nextInt();
}

ob.selectionSort(array);
System.out.println("Array after selection sort..");

for(int i=0;i<num;i++){
System.out.print(array[i] + " ");
}
}
}