0% found this document useful (0 votes)
6 views

Sorting Algos

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Sorting Algos

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Arrays;
public class MyClass {
public static void main(String args[]) {
int [] arr = {0,18,1,8,9,4,1,2,2,3,7,8};

//bubble sort
/*for(int i = 0 ; i < arr.length ; i++){
for(int j = i ; j < arr.length - 1; j++){
if(arr[j] > arr[j+1]){
swap(arr , j , j+1);
}
}
}*/

//selection sort
/* for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // Update minIndex if a smaller element is found
}
}
}
*/

//Insertion sort
for(int i = 0 ; i < arr.length-1; i++){
for(int j = i+1 ; j >0 ; j--){
if(arr[j] < arr[j-1]){
swap(arr , j , j-1);
}else{break;}
}
}

System.out.println(Arrays.toString(arr));
}
public static void swap(int arr[] , int i , int j){
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}

You might also like