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

All Sorting

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)
12 views

All Sorting

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

import java.util.

*;
class selection_sortasc
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,j,min,tmp;
System.out.println("enter the size");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter values in
array");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
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++)
{
System.out.println(a[i]);
}
}
}
import java.util.*;
class selection_sortdesc
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,j,max,tmp;
System.out.println("enter the size");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter values in
array");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
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++)
{
System.out.println(a[i]);
}
}
}
import java.util.*;
class bubble _sortasc
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,j,tmp=0;
System.out.println("enter size
array:");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter value
array");
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
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;
}
}
}
System.out.println("sorted array is:");
for(i=0;i<n;i++){
System.out.println(a[i]);
}
}
}
import java.util.*;
class bubble _sortdesc
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,j,tmp=0;
System.out.println("enter size
array:");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter value
array");
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
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;
}
}
}
System.out.println("sorted array is:");
for(i=0;i<n;i++){
System.out.println(a[i]);
}
}
}

You might also like