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

Array sorting

The document provides an explanation and implementation of two sorting algorithms: Bubble Sort and Selection Sort. It details the steps involved in each algorithm, including array initialization, comparisons, swaps, and the final sorted output. Both algorithms are illustrated with example arrays to demonstrate their sorting processes.

Uploaded by

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

Array sorting

The document provides an explanation and implementation of two sorting algorithms: Bubble Sort and Selection Sort. It details the steps involved in each algorithm, including array initialization, comparisons, swaps, and the final sorted output. Both algorithms are illustrated with example arrays to demonstrate their sorting processes.

Uploaded by

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

Bubble sorting

class BubbleSort {

public static void main(String args[])

int a[] = { 64, 34, 25, 12 };

int n = a.length;

System.out.print("Original array: ");

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

System.out.print(a[i]);

for (int i = 0; i < n - 1; i++){

for (int j = 0; j < n - i - 1; j++){

if (arr[j] > arr[j + 1]) {

// swap temp and arr[i]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

System.out.print("Sorted array: ");

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

System.out.print(a[i] + " ");

Code Explanation:

1. Array Initialization:

int a[] = { 64, 34, 25, 12 };

An array a is initialized with the values { 64, 34, 25, 12 }.


This is the array that will be sorted.

2. Array Length:

int n = a.length;

n stores the length of the array (4 in this case).

3. Outer Loop (Passes):

for (int i = 0; i < n - 1; i++)

The outer loop runs n - 1 times. Each iteration of this loop represents one "pass" through the
array.

After each pass, the largest unsorted element "bubbles up" to its correct position at the end of
the array.

4. Inner Loop (Comparisons):

for (int j = 0; j < n - i - 1; j++)

The inner loop compares adjacent elements in the array.

n - i - 1 ensures that the algorithm doesn't re-check the already sorted elements at the end of the
array.

5. Comparison and Swap:

if (a[j] > a[j + 1]) {

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

If the current element a[j] is greater than the next element a[j + 1], they are swapped.

This ensures that the larger element "bubbles up" toward the end of the array.

6. Print the Sorted Array:

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

System.out.print(a[i] + " ");

After sorting, the array is printed in ascending order.


How Bubble Sort Works:

First Pass:

Compare 64 and 34: Swap them (34, 64, 25, 12).

Compare 64 and 25: Swap them (34, 25, 64, 12).

Compare 64 and 12: Swap them (34, 25, 12, 64).

After the first pass, the largest element (64) is in its correct position.

Second Pass:

Compare 34 and 25: Swap them (25, 34, 12, 64).

Compare 34 and 12: Swap them (25, 12, 34, 64).

After the second pass, the second-largest element (34) is in its correct position.

Third Pass:

Compare 25 and 12: Swap them (12, 25, 34, 64).

After the third pass, the third-largest element (25) is in its correct position.

Final Array:

The array is now fully sorted: {12, 25, 34, 64}.

Selection Sort

public class SelectionSort{

public static void main(String[] args){

int[] arr = { 64, 25, 12, 22, 11 };

int n = arr.length;

System.out.print("Original array: ");

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

System.out.print(arr[i]+" ");

for (int i = 0; i < n - 1; i++) {

// Assume the current position holds


// the minimum element

int min_idx = i;

// Iterate through the unsorted portion

// to find the actual minimum

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

// Update min_idx if a smaller element

// is found

min_idx = j;

// Move minimum element to its

// correct position

int temp = arr[i];

arr[i] = arr[min_idx];

arr[min_idx] = temp;

System.out.print("Sorted array: ");

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

System.out.print(arr[i]+" ");

}
1.Initialize the array and print the original array:

• The arr array is initialized with the values {64, 25, 12, 22, 11}.

• The length of the array n is determined.

• The original array is printed.

2. Outer loop (for i from 0 to n-1):

• The outer loop iterates through each element of the array, considering it as the minimum
element (min_idx).

3. Inner loop (for j from i+1 to n):

• The inner loop compares the current minimum element with the remaining unsorted
elements in the array.

• If a smaller element is found, the index of the new minimum element (min_idx) is
updated.

4. Swap the minimum element with the current element:

• After the inner loop completes, the minimum element found is swapped with the element
at the current position (i).

• This ensures that the smallest element is placed at the beginning of the unsorted portion
of the array.

5. Repeat the process:

• Steps 2-4 are repeated for the next position until the entire array is sorted.

6. Print the sorted array:

• The sorted array is printed.

Original array: [64, 25, 12, 22, 11]

Step 1: [11, 25, 12, 22, 64] // Swap 64 with 11

Step 2: [11, 12, 25, 22, 64] // Swap 25 with 12

Step 3: [11, 12, 22, 25, 64] // Swap 25 with 22

Step 4: [11, 12, 22, 25, 64] // 25 is already in the correct position

Step 5: [11, 12, 22, 25, 64] // 64 is already in the correct position

You might also like