0% found this document useful (0 votes)
3 views5 pages

Java Sorting Algorithms Explained

Uploaded by

safdernowaz510
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)
3 views5 pages

Java Sorting Algorithms Explained

Uploaded by

safdernowaz510
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

import [Link].

*;
class selection_sortasc
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int n,i,j,min,tmp;
[Link]("enter the size");
n=[Link]();
int a[]=new int[n];
[Link]("enter values in
array");
for(i=0;i<n;i++)
{
a[i]=[Link]();
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j] < a[min])
min=j;
}
tmp=a[min];
a[min]=a[i];
a[i]=tmp;
}
for(i=0;i<n;i++)
{
[Link](a[i]);
}
}
}
import [Link].*;
class selection_sortdesc
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int n,i,j,max,tmp;
[Link]("enter the size");
n=[Link]();
int a[]=new int[n];
[Link]("enter values in
array");
for(i=0;i<n;i++)
{
a[i]=[Link]();
}
for(i=0;i<n-1;i++)
{
max=i;
for(j=i+1;j<n;j++)
{
if(a[j] > a[min])
max=j;
}
tmp=a[max];
a[max]=a[i];
a[i]=tmp;
}
for(i=0;i<n;i++)
{
[Link](a[i]);
}
}
}
import [Link].*;
class bubble _sortasc
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int i,n,j,tmp=0;
[Link]("enter size
array:");
n=[Link]();
int a[]=new int[n];
[Link]("enter value
array");
for(i=0;i<n;i++){
a[i]=[Link]();
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j] > a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
[Link]("sorted array is:");
for(i=0;i<n;i++){
[Link](a[i]);
}
}
}
import [Link].*;
class bubble _sortdesc
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int i,n,j,tmp=0;
[Link]("enter size
array:");
n=[Link]();
int a[]=new int[n];
[Link]("enter value
array");
for(i=0;i<n;i++){
a[i]=[Link]();
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j] < a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
[Link]("sorted array is:");
for(i=0;i<n;i++){
[Link](a[i]);
}
}
}

Common questions

Powered by AI

Bubble sort sorts an array by repeatedly passing through the list, comparing adjacent pairs, and swapping them if they are in the wrong order such that smaller elements "bubble" to the top for ascending order or vice versa for descending . This process ensures that, after each full pass of the array, the next-largest element occupies the correct position, and the sorted portion of the array increases from the back to front . The algorithm terminates once no swaps are needed during a complete pass through the list, indicating that the array is sorted .

The primary difference lies in their sorting mechanism. The selection sort algorithm iterates through the entire array and selects the minimum (or maximum for descending order) element from unsorted part and swaps it with the first unsorted element . This process is repeated moving the boundary of unsorted and sorted parts until the whole array is sorted . In contrast, bubble sort works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order, effectively "bubbling" the highest (or lowest, in descending sort) element to its correct position with each full pass through the list .

In the bubble sort algorithm, the inner loop is responsible for iterating over the array and performing the necessary comparisons and swaps . For each pair of adjacent elements, it checks if they are out of order (greater than for ascending, less than for descending) and performs a swap if they are. This process "bubbles" the largest unsorted element to the end of the unsorted portion of the array with each complete iteration of the inner loop .

In the descending version of selection sort, the algorithm needs to find the largest element rather than the smallest to place it at the correct position for a descending order arrangement . Therefore, it uses a 'greater than' (>) comparison instead of the 'less than' (<) used in the ascending version when determining the element to be moved to the front of the unsorted section .

The presented Java sorting programs do not include explicit error handling for input collection, assuming that the user inputs are correct and conform to the expected format (integers). Without error handling, these programs may encounter runtime errors if non-integer inputs are provided, halting execution and potentially causing issues like misbehaved sort outcomes or program crashes . Incorporating input validation would improve robustness against such input errors .

The error in the descending selection sort code lies in the variable name used in the comparison statement. It incorrectly uses 'min' instead of 'max' during the comparison inside the inner loop (`if(a[j] > a[min])`). It should be corrected to `if(a[j] > a[max])` to correctly identify the maximum value in each pass .

Temporary variables, such as 'tmp' used in both selection and bubble sort algorithms, aid in preventing data loss during element swapping . When two elements need to be swapped, the value of one element is stored in the temporary variable to avoid overwriting its value accidentally. This temporary storage allows the two values to be swapped correctly by holding one element safely in 'tmp' until both swappings involving the original position and the new position are complete .

When sorting an array initially sorted in descending order using bubble sort for ascending order, the algorithm will exhibit its worst-case time complexity, O(n^2), since every pair of adjacent elements would require swapping . Conversely, if the array were initially sorted in ascending order, no swaps would be necessary, and the algorithm could terminate early, achieving its best-case complexity of O(n). This stark contrast results from the need for numerous swaps to reverse every existing order relation in the worst-case scenario .

The selection sort algorithm sorts the array in ascending order by iteratively selecting the smallest element from the unsorted portion of the array and swapping it with the first element of the unsorted part . The index of the minimum element is updated each time it meets a smaller element during the inner loop iteration. Each selected smallest element is then placed at the beginning of the unsorted section, progressively building the sorted segment at the array's start .

Both bubble sort and selection sort have an average and worst-case time complexity of O(n^2) due to the nested iterations over the array elements . However, their operational efficiency differs in practice. Bubble sort can stop early if it detects that the array is already sorted after a pass without swaps, providing a small runtime advantage in best-case scenarios, making its best-case complexity O(n). Selection sort consistently checks all elements for each pass, making it less adaptive than bubble sort .

You might also like