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

selection sort ascending and descending

The document contains Java programs for sorting arrays using selection sort and bubble sort algorithms in both ascending and descending order. Each sorting method is implemented in separate classes, with user input for an array of 10 integers. The programs demonstrate the differences in logic for sorting in ascending versus descending order.

Uploaded by

heyrayyan2024
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)
4 views

selection sort ascending and descending

The document contains Java programs for sorting arrays using selection sort and bubble sort algorithms in both ascending and descending order. Each sorting method is implemented in separate classes, with user input for an array of 10 integers. The programs demonstrate the differences in logic for sorting in ascending versus descending order.

Uploaded by

heyrayyan2024
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/ 2

Program to sort using selection sort in ASCENDING Program to sort using selection sort in DESCENDING

ORDER.. ORDER..
import java.util.*; import java.util.*;
class selection class selection
{ {
public static void selectionsort(int[] a) public static void selectionsort(int[] a)
{ {
for(int i=0;i<a.length-1;i++) for(int i=0;i<a.length-1;i++)
{ {
int small=i; small is changed int big=i;
for(int j=i+1;j<a.length;j++) to big and in if for(int j=i+1;j<a.length;j++)
{ condition sign is {
if(a[small]>a[j]) changed <
if(a[big] a[j])
small=j; big=j;
} }
//a[small],a[i] swap //a[big],a[i] swap
int temp=a[small]; int temp=a[big];
a[small]=a[i]; a[big]=a[i];
a[i]=temp; a[i]=temp;
} }
} }
public static void main(String args[]) public static void main(String args[])
{ {
int[] a=new int[10]; int[] a=new int[10];
Scanner sc=new Scanner(System.in); Scanner sc=new Scanner(System.in);
System.out.println("enter 10 integers"); System.out.println("enter 10 integers");
for(int i=0;i<10;i++) for(int i=0;i<10;i++)
{ {
a[i]=sc.nextInt(); a[i]=sc.nextInt();
} }
selectionsort(a); selectionsort(a);
System.out.println("sorted array is:"); System.out.println("sorted array is:");
for(int i=0;i<10;i++) for(int i=0;i<10;i++)
{ {
System.out.print(a[i]+" "); System.out.print(a[i]+" ");
} }
} }
} }
Program to sort using bubble sort in ASCENDING Program to sort using bubble sort in DESCENDING
ORDER.. ORDER..
import java.util.*; import java.util.*;
class Bubble class Bubble
{ {
public static void bubblesort(int[] a) public static void bubblesort(int[] a)
{ {
for(int i=0;i<a.length-1;i++) for(int i=0;i<a.length-1;i++)
{ {
for(int j=0;j<a.length-1-i;j++) in if condition for(int j=0;j<a.length-1-i;j++)
{ sign is changed {
if(a[j]>a[j+1]){ if(a[j]<a[j+1]){
//a[j],a[j+1] swap
//a[j],a[j+1] swap
int temp=a[j];
int temp=a[j];
a[j]=a[j+1];
a[j]=a[j+1];
a[j+1]=temp;
a[j+1]=temp;
}
}
}}}
}}}
public static void main(String args[])
public static void main(String args[])
{
{
int[] a=new int[10];
int[] a=new int[10];
Scanner sc=new Scanner(System.in);
Scanner sc=new Scanner(System.in);
System.out.println("enter 10 integers");
System.out.println("enter 10 integers");
for(int i=0;i<10;i++)
for(int i=0;i<10;i++)
{
{
a[i]=sc.nextInt();
a[i]=sc.nextInt();
}
}
bubblesort(a);
bubblesort(a);
System.out.println("sorted array is:");
System.out.println("sorted array is:");
for(int i=0;i<10;i++)
for(int i=0;i<10;i++)
{
{
System.out.print(a[i]+" ");
System.out.print(a[i]+" ");
}
}
}
}
}
}

You might also like