Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Searching and Sorting Algorithm Programs

The document contains Java code for selection sort, bubble sort, and binary search algorithms to sort and search arrays in ascending and descending order. Selection sort and bubble sort are used to sort integer arrays and string arrays based on population data. Binary search is applied to sorted integer arrays to find a target number.

Uploaded by

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

Searching and Sorting Algorithm Programs

The document contains Java code for selection sort, bubble sort, and binary search algorithms to sort and search arrays in ascending and descending order. Selection sort and bubble sort are used to sort integer arrays and string arrays based on population data. Binary search is applied to sorted integer arrays to find a target number.

Uploaded by

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

/**Java program to arrange an array in ASSCENDING order using, Exchange

Selection Sort */
class ExSelSort_Asc
{
static void Arrange(int Arr[])
{
int i=0,j=0,Val=0,Pos=0,Temp=0,len=Arr.length;
for(i=0;i<len;i++)
{
Val=Arr[i]; //to store current element
Pos=i; //position of current element
for(j=i+1;j<len;j++)
if(Arr[j]<Val)
{
Val=Arr[j];
Pos=j;
}
Temp=Arr[i];
Arr[i]=Arr[Pos];
Arr[Pos]=Temp;
}
System.out.println("Sorted Array: ");
for(i=0;i<len;i++)
System.out.print(Arr[i]+", ");
}
}
/**Java program to arrange an array in DESCENDING order using, Exchange
Selection Sort */
class ExSelSort_Desc
{
static void Arrange(int Arr[])
{
int i=0,j=0,Val=0,Pos=0,Temp=0,len=Arr.length;

for(i=0;i<len;i++)
{
Val=Arr[i]; //to store current element
Pos=i; //position of current element
for(j=i+1;j<len;j++)
if(Arr[j]>Val)
{
Val=Arr[j];
Pos=j;
}
Temp=Arr[i];
Arr[i]=Arr[Pos];
Arr[Pos]=Temp;
}
System.out.println("Sorted Array: ");
for(i=0;i<len;i++)
System.out.print(Arr[i]+", ");
}
}
/** Java program to input name of cities and their population in 2 different arrays
and arrange all cities in Ascending order (according to their population) */
class BubbleSort_Asc
{
static void Print(String Nm[],int Arr[])
{
int i=0,j=0,len=Arr.length;
System.out.println("\tUN-Sorted Array\nCity\t\tPopulation");
for(i=0;i<len;i++)
System.out.println(Nm[i]+"\t\t"+Arr[i]);

for(i=0;i<len;i++)
for(j=0;j<len-1-i;j++)
if(Arr[j]>Arr[j+1])
{
int Temp=Arr[j];
Arr[j]=Arr[j+1];
Arr[j+1]=Temp;

String str=Nm[j];
Nm[j]=Nm[j+1];
Nm[j+1]=str;
}
System.out.println("\n\n\tSorted Array\nCity\t\tPopulation");
for(i=0;i<len;i++)
System.out.println(Nm[i]+"\t\t"+Arr[i]);
}
}
/** Java program to input name of cities and their population in 2 different arrays
and arrange all cities in Descending order (according to their population) */

class BubbleSort_Desc
{
static void Print(String Nm[],int Arr[])
{
int i=0,j=0,len=Arr.length;
System.out.println("\tUN-Sorted Array\nCity\t\tPopulation");
for(i=0;i<len;i++)
System.out.println(Nm[i]+"\t\t"+Arr[i]);

for(i=0;i<len;i++)
for(j=0;j<len-1-i;j++)
if(Arr[j]<Arr[j+1])
{
int Temp=Arr[j];
Arr[j]=Arr[j+1];
Arr[j+1]=Temp;

String str=Nm[j];
Nm[j]=Nm[j+1];
Nm[j+1]=str;
}
System.out.println("\n\n\tSorted Array\nCity\t\tPopulation");
for(i=0;i<len;i++)
System.out.println(Nm[i]+"\t\t"+Arr[i]);
}
}
/** Write a java program to input an array (in ascending order) and a number
"Num", Search inputted array for "Num", using "Binary Search Algorithm" */
class Binary_Search_Asc
{
static void Search(int Arr[], int Num)
{
int L=0, M=0, U=Arr.length-1, Flag=0;
while(L<=U)
{
M=(L+U)/2;
if(Num>Arr[M])
L=M+1;
else if(Num<Arr[M])
U=M-1;
else if(Num==Arr[M])
{
Flag=1;
break;
}
}
if(Flag==1)
System.out.println(Num+" found at index- "+M);
else
System.out.println(Num+" not found");
}
}
/** Write a java program to input an array (in descending order) and a number
"Num", Search inputted array for "Num", using "Binary Search Algorithm" */
class Binary_Search_Desc
{
static void Search(int Arr[], int Num)
{
int L=0, M=0, U=Arr.length-1, Flag=0;
while(L<=U)
{
M=(L+U)/2;
if(Num>Arr[M])
U=M-1;
else if(Num<Arr[M])
L=M+1;
else if(Num==Arr[M])
{
Flag=1;
break;
}
}
if(Flag==1)
System.out.println(Num+" found at index- "+M);
else
System.out.println(Num+" not found");
}
}

You might also like