0% found this document useful (0 votes)
33 views2 pages

Java Sorting Algorithms Explained

The document provides Java implementations for various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort, Counting Sort, and Radix Sort. Each algorithm is presented with its respective code snippet. The document serves as a reference for understanding and implementing these sorting techniques in Java.

Uploaded by

chetanchelak52
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)
33 views2 pages

Java Sorting Algorithms Explained

The document provides Java implementations for various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort, Counting Sort, and Radix Sort. Each algorithm is presented with its respective code snippet. The document serves as a reference for understanding and implementing these sorting techniques in Java.

Uploaded by

chetanchelak52
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 Algorithms - Java Code

1. Bubble Sort
--------------
int[] bubbleSort(int[] a) {
for(int i=0; i<[Link]; i++){
for(int j=0; j<[Link]-1; j++){
if(a[j] > a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}

2. Selection Sort
-----------------
int[] selectionSort(int[] a) {
for(int i=0; i<[Link]; i++){
int min = i;
for(int j=i+1; j<[Link]; j++){
if(a[j] < a[min]) min = j;
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
return a;
}

3. Insertion Sort
-----------------
int[] insertionSort(int[] a) {
for(int i=1; i<[Link]; i++){
int key = a[i];
int j = i-1;
while(j >= 0 && a[j] > key){
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
return a;
}

4. Merge Sort
-------------
void mergeSort(int[] a, int left, int right){
if(left >= right) return;
int mid = (left + right) / 2;
mergeSort(a, left, mid);
mergeSort(a, mid+1, right);
merge(a, left, mid, right);
}

5. Quick Sort
-------------
int partition(int[] a, int low, int high){
int pivot = a[high];
int i = low;
for(int j=low; j<high; j++){
if(a[j] < pivot){
int temp = a[i]; a[i] = a[j]; a[j] = temp;
i++;
}
}
int temp = a[i]; a[i] = a[high]; a[high] = temp;
return i;
}
6. Heap Sort
------------
void heapify(int[] arr, int n, int i){
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;

if(l < n && arr[l] > arr[largest]) largest = l;


if(r < n && arr[r] > arr[largest]) largest = r;

if(largest != i){
int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp;
heapify(arr, n, largest);
}
}

7. Counting Sort
----------------
int[] countingSort(int[] a, int max){
int[] count = new int[max+1];
for(int num : a) count[num]++;
int idx = 0;
for(int i=0; i<[Link]; i++){
while(count[i]-- > 0) a[idx++] = i;
}
return a;
}

8. Radix Sort
-------------
int findMax(int[] a){
int max = a[0];
for(int i=1; i<[Link]; i++) if(a[i] > max) max = a[i];
return max;
}

You might also like