0% found this document useful (0 votes)
2 views3 pages

Sorting -Updated

The document provides Java code implementations for various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, and Radix Sort. Each algorithm is presented with its respective method and a sample array to demonstrate sorting functionality. The document is intended for training purposes at the JSTC (Java & Python Training Centre) in Bhopal.

Uploaded by

anshikaaneja2
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 views3 pages

Sorting -Updated

The document provides Java code implementations for various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, and Radix Sort. Each algorithm is presented with its respective method and a sample array to demonstrate sorting functionality. The document is intended for training purposes at the JSTC (Java & Python Training Centre) in Bhopal.

Uploaded by

anshikaaneja2
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
You are on page 1/ 3

1

JSTC (Java & Python Training Centre)


Sorting-Algorithms: }
static void printArray(int[] arr){
for (int val : arr) {
Bubble-sort:
System.out.print(val + " ");
package Pack1;
}
import java.util.Arrays;
System.out.println();
}
class A {
public static void main(String[] args){
static void bubbleSort(int array[]) {
int[] arr = { 64, 25, 12, 22, 11 };
int size = array.length;
System.out.print("Original array: ");
for (int i = 0; i < size - 1; i++)
printArray(arr);
for (int j = 0; j < size - i - 1; j++)
selectionSort(arr);
if (array[j] > array[j + 1]) {
System.out.print("Sorted array: ");
int temp = array[j];
printArray(arr);
array[j] = array[j + 1];
}
array[j + 1] = temp;
}
}
} Insertion-sort:
public class A {
public static void main(String args[]) { void sort(int arr[])
int[] data = { 5, 3, 4, 1, 2 }; {
A.bubbleSort(data); int n = arr.length;
for (int i = 1; i < n; ++i) {
System.out.println("Array sorted with bubble int key = arr[i];
sort: "); int j = i - 1;
System.out.println(Arrays.toString(data)); while (j >= 0 && arr[j] > key) {
} arr[j + 1] = arr[j];
} j = j - 1;
}
Selection-sort:
arr[j + 1] = key;
package Pack1;
}
import java.util.Arrays;
}
static void printArray(int arr[])
class A {
{
int n = arr.length;
static void selectionSort(int[] arr){
for (int i = 0; i < n; ++i)
int n = arr.length;
System.out.print(arr[i] + " ");
for (int i = 0; i < n - 1; i++) {
System.out.println();
int min_idx = i;
}
public static void main(String args[])
for (int j = i + 1; j < n; j++) {
{
if (arr[j] < arr[min_idx]) {
int arr[] = { 12, 11, 13, 5, 6 };
min_idx = j;
}
A ob = new A();
}
ob.sort(arr);
int temp = arr[i];
arr[i] = arr[min_idx];
printArray(arr);
arr[min_idx] = temp;
}
}
JSTC, E-3/285, Arera Colony,10 No. Market, Near SBI, Bhopal
Contact: 9589016060
2
JSTC (Java & Python Training Centre)
} Merge-sort:
Quick-sort: package Pack1;
package Pack1; import java.io.*;
import java.util.Arrays;
class A{
class A { static void merge(int arr[], int l, int m, int r)
{
static int partition(int[] arr, int low, int high) { int n1 = m - l + 1;
int pivot = arr[high]; int n2 = r - m;
int L[] = new int[n1];
int i = low - 1; int R[] = new int[n2];
for (int j = low; j <= high - 1; j++) { for (int i = 0; i < n1; ++i)
if (arr[j] < pivot) { L[i] = arr[l + i];
i++; for (int j = 0; j < n2; ++j)
swap(arr, i, j); R[j] = arr[m + 1 + j];
} int i = 0, j = 0;
} int k = l;
while (i < n1 && j < n2) {
swap(arr, i + 1, high); if (L[i] <= R[j]) {
return i + 1; arr[k] = L[i];
} i++;
static void swap(int[] arr, int i, int j) { }
int temp = arr[i]; else {
arr[i] = arr[j]; arr[k] = R[j];
arr[j] = temp; j++;
} }
k++;
static void quickSort(int[] arr, int low, int }
high) { while (i < n1) {
if (low < high) { arr[k] = L[i];
int pi = partition(arr, low, high); i++;
quickSort(arr, low, pi - 1); k++;
quickSort(arr, pi + 1, high); }
} while (j < n2) {
} arr[k] = R[j];
j++;
public static void main(String[] args) { k++;
int[] arr = {10, 7, 8, 9, 1, 5}; }
int n = arr.length; }
static void sort(int arr[], int l, int r)
quickSort(arr, 0, n - 1); {
if (l < r) {
for (int val : arr) {
System.out.print(val + " "); int m = l + (r - l) / 2;
}
} sort(arr, l, m);
} sort(arr, m + 1, r);

JSTC, E-3/285, Arera Colony,10 No. Market, Near SBI, Bhopal


Contact: 9589016060
3
JSTC (Java & Python Training Centre)
merge(arr, l, m, r); for (i = n - 1; i >= 0; i--) {
} output[count[(arr[i] / exp) % 10] - 1] =
} arr[i];
static void printArray(int arr[]) count[(arr[i] / exp) % 10]--;
{ }
int n = arr.length;
for (int i = 0; i < n; ++i) for (i = 0; i < n; i++)
System.out.print(arr[i] + " "); arr[i] = output[i];
System.out.println(); }
}
public static void main(String args[]) static void radixsort(int arr[], int n)
{ {
int arr[] = { 12, 11, 13, 5, 6, 7 }; int m = getMax(arr, n);
System.out.println("Given array is");
printArray(arr); for (int exp = 1; m / exp > 0; exp *= 10)
sort(arr, 0, arr.length - 1); countSort(arr, n, exp);
System.out.println("\nSorted array is"); }
printArray(arr); static void print(int arr[], int n)
} {
} for (int i = 0; i < n; i++)
Redix short System.out.print(arr[i] + " ");
package Pack1; }
import java.io.*; public static void main(String[] args)
import java.util.*; {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
class A { int n = arr.length;
radixsort(arr, n);
static int getMax(int arr[], int n) print(arr, n);
{ }
int mx = arr[0]; }
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
static void countSort(int arr[], int n, int exp)
{
int output[] = new int[n];
int i;
int count[] = new int[10];
Arrays.fill(count, 0);

for (i = 0; i < n; i++)


count[(arr[i] / exp) % 10]++;

for (i = 1; i < 10; i++)


count[i] += count[i - 1];

JSTC, E-3/285, Arera Colony,10 No. Market, Near SBI, Bhopal


Contact: 9589016060

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy