0% found this document useful (0 votes)
2 views1 page

Sorting

The document contains implementations of three sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort in Java. Each algorithm is defined within a class named 'Solution' and includes methods to sort an array of integers. The code snippets demonstrate the logic for swapping elements and maintaining order during the sorting process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Sorting

The document contains implementations of three sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort in Java. Each algorithm is defined within a class named 'Solution' and includes methods to sort an array of integers. The code snippets demonstrate the logic for swapping elements and maintaining order during the sorting process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SORTING if (nums[j] < nums[minIndex]) {

minIndex = j;
// update minimum index
BUBBLE SORT }}
// swap
int temp = nums[minIndex];
nums[minIndex] = nums[i];
nums[i] = temp;
}
return nums;
}}

class Solution {
public int[] bubbleSort(int[] nums) {
int size=[Link];
int temp=0;

for(int i = 0 ; i < size ; i++) {


for(int j =0;j<size-i-1;j++){
if(nums[j+1]>nums[j]){
temp=nums[j];
nums[j+1]=nums[j];
temp=nums[j+1];
}}}}}
INSERTION SORT
class Solution {
public int[] insertionSort(int[] nums){
for(int i = 1; i < [Link]; i++){
int key = nums[i];
int j = i - 1;

while(j >= 0 && nums[j] > key) {


nums[j + 1] = nums[j];
j--;
}
nums[j + 1] = key;
}
return nums;
}}
SELECTION SORT
class Solution {
public int[] selectionSort(int[] nums) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
// assume current index has minimum
for (int j = i + 1; j < n; j++) {

You might also like