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

Sorting in Java - 3 Techniques

The document discusses three sorting algorithms in Java: 1. Bubble sort, which compares adjacent elements and swaps them if out of order, with O(N2) time complexity. 2. Selection sort, which finds the minimum element and places it in the correct position each iteration, also with O(N2) time complexity. 3. Insertion sort, which inserts elements into the sorted portion of the array one by one, shifting other elements as needed, also with O(N2) time complexity.

Uploaded by

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

Sorting in Java - 3 Techniques

The document discusses three sorting algorithms in Java: 1. Bubble sort, which compares adjacent elements and swaps them if out of order, with O(N2) time complexity. 2. Selection sort, which finds the minimum element and places it in the correct position each iteration, also with O(N2) time complexity. 3. Insertion sort, which inserts elements into the sorted portion of the array one by one, shifting other elements as needed, also with O(N2) time complexity.

Uploaded by

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

Sorting in JAVA

1. Bubble Sort
Idea: if arr[i] > arr[i+1] swap them. To place the element in their
respec ve posi on, we have to do the following opera on N-1
mes.
Time Complexity: O(N2)

Code
import java.util.*;

class Sorting {
public static void printArray(int arr[]) {
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}

public static void main(String args[]) {


int arr[] = {7, 8, 1, 3, 2};

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

printArray(arr);
}
}

2. Selection Sort
Idea: The inner loop selects the minimum element in the
unsorted array and places the elements in increasing order.
Time complexity: O(N2)

Code
import java.util.*;

class Sorting {
public static void printArray(int arr[]) {
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}

public static void main(String args[]) {


int arr[] = {7, 8, 1, 3, 2};

//selection sort
for(int i=0; i<arr.length-1; i++) {
int smallest = i;
for(int j=i+1; j<arr.length; j++) {
if(arr[j] < arr[smallest]) {
smallest = j;
}
}
//swap
int temp = arr[smallest];
arr[smallest] = arr[i];
arr[i] = temp;
}

printArray(arr);
}
}

3. Insertion Sort
Idea: Take an element from the unsorted array, place it in its
corresponding posi on in the sorted part, and shi the elements
accordingly.
Time Complexity: O(N2)

Code
import java.util.*;

class Sorting {
public static void printArray(int arr[]) {
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}

public static void main(String args[]) {


int arr[] = {7, 8, 1, 3, 2};

//insertion sort
for(int i=1; i<arr.length; i++) {
int current = arr[i];
int j = i - 1;
while(j >= 0 && arr[j] > current) {
//Keep swapping
arr[j+1] = arr[j];
j--;
}
arr[j+1] = current;
}
printArray(arr);
}
}
`

You might also like