ARRAYS PROGRAMMING
1. Selection sort of number in Descending Order.
import [Link];
public class Descending_Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Descending Order:");
for (int i = 0; i < n - 1; i++)
{
[Link](a[i] + ",");
}
[Link](a[n - 1]);
}
}
2. Selection sort of string in Descending order.
import [Link];
public class SelectionSortString
{
public static void main(String[] args)
{
String cities[] = new String[15];
Scanner sc = new Scanner([Link]);
int l = [Link];
[Link]("Enter 15 cities name:");
for (int i = 0; i < l; i++)
{
cities[i]=[Link]();
}
[Link]();
for (int i = 0; i < l - 1; i++)
{
int min = i;
String st = cities[i];
for (int j = i + 1; j < l; j++)
{
if (cities[j].compareTo(st) > 0)
{
st = cities[j];
min = j;
}
}
if (min != i)
{
String temp = cities[min];
cities[min] = cities[i];
cities[i] = temp;
}
}
[Link]("Sorted cities name:");
for (int i = 0; i < l; i++)
{
[Link](cities[i]);
}
}
}
3. Bubble sort of numbers in descending order
import [Link];
class BubbleSortExample {
public static void main(String []args) {
int num, i, j, temp;
Scanner input = new Scanner([Link]);
[Link]("Enter the number of integers to sort:");
num = [Link]();
int array[] = new int[num];
[Link]("Enter " + num + " integers: ");
for (i = 0; i < num; i++)
array[i] = [Link]();
for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num - i - 1; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
[Link]("Sorted list of integers:");
for (i = 0; i < num; i++)
[Link](array[i]);
}
4. Bubble sort of string in Ascending order
public class BubbleSort {
public static void main(String[] args) {
int i,j;
String s_arr[] = {
"Richard",
"John",
"Williams",
"Peter",
"Aron"
};
String tmp;
[Link]("Sorted Strings:");
for ( i = 0 ; i < s_arr.length ; i++) {
for ( j = 0 ; j < s_arr.length-i-1 ; j++) {
if (s_arr[j+1].compareTo(s_arr[j]) > 0) {
tmp = s_arr[j];
s_arr[j] = s_arr[j+1];
s_arr[j+1] = tmp;
}
}
[Link](s_arr[j]);
}
}
}
OUTPUT
Sorted Strings:
Aron
John
Peter
Richard
Williams