Sorting
Sorting
Bubble sort
Watch video : https://www.youtube.com/watch?v=CTqKGxx7LtE&t=1s
Explanation :
int n = arr.length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
}
}
}
}
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
System.out.println("Sorted array:");
bs.printArray(arr);
}
}
1B) BUBBLE SORT PSEUDOCODE
{
// A function to implement bubble sort
static void bubbleSort(int arr[], int n)
{
// Base case
if (n == 1)
return;
int count = 0;
{
// swap arr[i], arr[i+1]
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
count = count+1;
}
if (count == 0)
return;
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);
Selection Sort
Watch Video : https://www.youtube.com/watch?v=8uO662LA98o
Explanation :
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
class Test
{
// Return minimum index
a[index] = temp;
}
// Recursively calling selection sort function
recurSelectionSort(a, n, index + 1);
}
// Driver method
public static void main(String args[])
{
// Calling function
recurSelectionSort(arr, arr.length, 0);