Sorting
Bubble sort
Watch video : https://www.youtube.com/watch?v=CTqKGxx7LtE&t=1s
Explanation :
1 A ) Bubble sort JAVA CODE
public 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])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[])
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
BubbleSort bs = new BubbleSort();
System.out.println("Original array:");
bs.printArray(arr);
// Call the sorting function
bs.bubbleSort(arr);
System.out.println("Sorted array:");
bs.printArray(arr);
}
}
1B) BUBBLE SORT PSEUDOCODE
1C) ) BUBBLE SORT USING RECUSRION
import java.util.Arrays;
public class GFG
{
// A function to implement bubble sort
static void bubbleSort(int arr[], int n)
{
// Base case
if (n == 1)
return;
int count = 0;
// One pass of bubble sort. After
// this pass, the largest element
// is moved (or bubbled) to end.
for (int i=0; i<n-1; i++)
if (arr[i] > arr[i+1])
{
// swap arr[i], arr[i+1]
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
count = count+1;
}
// Check if any recursion happens or not
// If any recursion is not happen then return
if (count == 0)
return;
// Largest element is fixed,
// recur for remaining array
bubbleSort(arr, n-1);
}
// Driver Method
public static void main(String[] args)
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr, arr.length);
System.out.println("Sorted array : ");
System.out.println(Arrays.toString(arr));
}
}
Selection Sort
Watch Video : https://www.youtube.com/watch?v=8uO662LA98o
Explanation :
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from the unsorted part
and putting it at the beginning.
Algorithm for Selection Sort
Implementation of Selection Sort in Java is mentioned below:
Step 1: Array arr with N size
Step 2: Initialise i=0
Step 3: If(i<N-1) Check for any element arr[j] where j>i and arr[j]<arr[i]
then Swap arr[i] and arr[j]
Step 4: i=i+1 and Goto Step 3
Step 5: Exit
2A) Selection Sort Java code
public class SelectionSort
{
void selectionSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n - 1; i++)
{
int minIndex = i; // Assume the minimum is at index i
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j; // Update minIndex if a smaller element is found
}
}
// Swap
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
// Main method
public static void main(String args[])
{
int arr[] = {64, 25, 12, 22, 11};
SelectionSort ss = new SelectionSort();
System.out.println("Original array:");
ss.printArray(arr);
ss.selectionSort(arr);
System.out.println("Sorted array:");
ss.printArray(arr);
}
}
2B) Selection Sort Pseudocode
2C) Selection Sort using Recursion
// Recursive Java program to sort an array
// using selection sort
class Test
{
// Return minimum index
static int minIndex(int a[], int i, int j)
{
if (i == j)
return i;
// Find minimum of remaining elements
int k = minIndex(a, i + 1, j);
// Return minimum of current and remaining.
return (a[i] < a[k])? i : k;
// Recursive selection sort. n is size of a[] and index
// is index of starting element.
static void recurSelectionSort(int a[], int n, int index)
// Return when starting and size are same
if (index == n)
return;
// calling minimum index function for minimum index
int k = minIndex(a, index, n-1);
// Swapping when index and minimum index are not same
if (k != index){
// swap
int temp = a[k];
a[k] = a[index];
a[index] = temp;
}
// Recursively calling selection sort function
recurSelectionSort(a, n, index + 1);
}
// Driver method
public static void main(String args[])
{
int arr[] = {3, 1, 5, 2, 7, 0};
// Calling function
recurSelectionSort(arr, arr.length, 0);
//printing sorted array
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
}
}