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

Sorting

Uploaded by

Veer Bahadur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Sorting

Uploaded by

Veer Bahadur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Write a program to sort elements of an array.

public class sort


{
public static void main(String[] args)
{
int[] arr = new int[] {78, 34, 1, 3, 90, 34, -1, -4, 6, 55, 20, -65};
System.out.println("Array elements after sorting:");
for (int i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
int tmp = 0;
if (arr[i] > arr[j])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
System.out.println(arr[i]);
}
}
}
2. WAP to sort elements of an array in descending order with user.
import java.util.Scanner;
public class sortdesc
{
public static void main()
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
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;
}
}
}
System.out.println("Array elements in descending order:");
for (int i = 0; i < n - 1; i++)
{
System.out.println(a[i]);
}
System.out.print(a[n - 1]);
}
}
3. Write a program to input 15 integer elements in an array and sort them in
ascending order using the bubble sort technique.

import java.util.Scanner;
public class bubblesort
{
public static void main()
{
Scanner in = new Scanner(System.in);
int n = 15;
int arr[] = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
arr[i] = in.nextInt();
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
}
}
4. Write a program to perform insertion sort based on user’s value.
import java.util.Scanner;
public class insertion
{
public static void main()
{
int n, i, j, element;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Size of Array: ");
n = scan.nextInt();
int[] arr = new int[n];
System.out.print("Enter "+n+" Elements: ");
for(i=0;i<n;i++)
arr[i] = scan.nextInt();
for(i=1;i<n;i++)
{
element = arr[i];
for(j=(i-1);j>=0 && arr[j]>element; j--)
arr[j+1] = arr[j];
arr[j+1] = element;
}
System.out.println("\nThe new sorted array is: ");
for(i=0; i<n; i++)
System.out.print(arr[i]+ " ");
}
}
5. Write a program to perform selection sort on the bases of user’s input.
import java.util.Scanner;
public class selection
{
public static void main()
{
int tot, i, j, count, small, index=0, x;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Size of Array: ");
tot = scan.nextInt();
int[] arr = new int[tot];
System.out.print("Enter " +tot+ " Elements for the Array: ");
for(i=0; i<tot; i++)
arr[i] = scan.nextInt();
for(i=0; i<(tot-1); i++)
{
count=0;
small = arr[i];
for(j=(i+1); j<tot; j++)
{
if(small>arr[j])
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)
{
x = arr[i];
arr[i] = small;
arr[index] = x;
}
}
System.out.println("\nThe new sorted array is: ");
for(i=0; i<tot; i++)
System.out.print(arr[i]+ " ");
}
}
6. Write a program to find and print the largest number in an array of 10
numbers.
import java.util.Scanner;
public class largest
{
public static void main()
{
int i, large;
int[] arr = new int[10];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 10 Numbers: ");
for(i=0; i<10; i++)
arr[i] = scan.nextInt();
large = arr[0];
for(i=1; i<10; i++)
{
if(large<arr[i])
large = arr[i];
}
System.out.println("\nLargest Number = " +large);
}
}
7. Write a program to insert an element in an array at the end.
import java.util.Scanner;
public class insertvariable
{
public static void main()
{
int i, element;
int[] arr = new int[11];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 10 Elements: ");
for(i=0; i<10; i++)
arr[i] = scan.nextInt();
System.out.print("Enter an Element to Insert: ");
element = scan.nextInt();
arr[i] = element;
System.out.println("\nNow the new array is: ");
for(i=0; i<11; i++)
System.out.print(arr[i]+ " ");
}
}
8. Write a program to Merge two array without user’s input.
import java.util.Scanner;
public class MergeArr
{
public static void main()
{
int arr1[]={1,2,3,4,5};
int arr2[]={6,7,8,9,10};
int arr3[]=new int[10];
int i, j, k=0;
System.out.println("Elements of first Array");
for(i=0; i<5; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.println("Elements of second Array");
for(i=0; i<5; i++)
{
System.out.print(arr2[i] + " ");
}
System.out.println();
for(i=0; i<5; i++)
{
arr3[k] = arr1[i];
k++;
}
for(i=0; i<5; i++)
{
arr3[k]=arr2[i];
k++;
}
System.out.println("After Merging of Two arrays into third array");
for(i=0; i<k; i++)
{
System.out.print(arr3[i] + " ");
}
System.out.println();
}
}
9. Write a program to merge to arrays using user’s input.
import java.util.Scanner;
public class margearr
{
public static void main(String[] args)
{
int i, k=0;
int[] merge = new int[100];
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of first array: ");
int x = scan.nextInt();
int[] a = new int[x];
System.out.print("Enter " +x+ " elements for first array: ");
for(i=0; i<x; i++)
{
a[i] = scan.nextInt();
merge[k] = a[i];
k++;
}
System.out.print("\nEnter the size of second array: ");
int y = scan.nextInt();
int[] b = new int[y];
System.out.print("Enter " +y+ " elements for second array: ");
for(i=0; i<y; i++)
{
b[i] = scan.nextInt();
merge[k] = b[i];
k++;
}
System.out.println("\nThe merged array is: ");
for(i=0; i<k; i++)
System.out.print(merge[i]+ " ");
}
}
10. Write a program to merge to array’s and sort their elements after
merging.
import java.util.Scanner;
public class mergesort
{
public static void main()
{
int i, j;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of first array: ");
int x = scan.nextInt();
int[] a = new int[x];
System.out.print("Enter " +x+ " elements for first array: ");
for(i=0; i<x; i++)
a[i] = scan.nextInt();
System.out.print("\nEnter the size of second array: ");
int y = scan.nextInt();
int[] b = new int[y];
System.out.print("Enter " +y+ " elements for second array: ");
for(i=0; i<y; i++)
b[i] = scan.nextInt();
int[] merge = new int[x+y];
for(i=0; i<x; i++)
merge[i] = a[i];
for(j=0; j<y; j++)
merge[i++] = b[j];
int k = i;
for(i=0; i<(k-1); i++)
{
for(j=0; j<(k-1); j++)
{
if(merge[j]>merge[j+1])
{
int temp = merge[j];
merge[j] = merge[j+1];
merge[j+1] = temp;
}
}
}
System.out.println("\nThe merged array is: ");
for(i=0; i<k; i++)
{
System.out.print(merge[i]+ " ");
}
}
}
11. Write a program to merge to two strings without using user’s input.
import java.util.Scanner;
public class StringMerge
{
public static void main()
{
String arr1[]={"Doremon", "Gian", "Rudra", "Bheem", "John"};
String arr2[]={"Novita", "Sinchain", "Golu", "Dholu", "Molu"};
String arr3[]=new String[10];
int i, j, k=0;
System.out.println("First Array");
for(i=0; i<5; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.println("\nSecond Array");
for(i=0; i<5; i++)
{
System.out.print(arr2[i] + " ");
}
System.out.println();
for(i=0; i<5; i++)
{
arr3[k]=arr1[i];
k++;
}
for(i=0; i<5; i++)
{
arr3[k]=arr2[i];
k++;
}
System.out.println("\nAfter Merging of Two arrays into third array");
for(i=0; i<k; i++)
{
System.out.print(arr3[i] + " ");
}
System.out.println();
}
}
12. Write a program to delete an array of an element and print remain
elements of array.
import java.util.Scanner;
public class deletearr
{
public static void main()
{
int i, j, size=10, element;
int[] arr = new int[size];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 10 Elements: ");
for(i=0; i<size; i++)
arr[i] = scan.nextInt();
System.out.print("Enter the Element to Remove: ");
element = scan.nextInt();
for(i=0; i<size; i++)
{
if(element==arr[i])
{
for(j=i; j<(size-1); j++)
arr[j] = arr[j+1];
System.out.println("\nRemoved the element successfully!");
break;
}
}
System.out.println("\nThe new array is: ");
for(i=0; i<(size-1); i++)
System.out.print(arr[i]+ " ");
}
}
13. WAP to find common elements in two array’s
import java.util.Scanner;
public class Commonele
{
public static void main()
{
int[] arrOne = new int[5];
int[] arrTwo = new int[5];
int i, j;
Scanner s = new Scanner(System.in);
System.out.print("Enter 5 elements for the first array: ");
for(i=0; i<5; i++)
arrOne[i] = s.nextInt();
System.out.print("\nEnter 5 elements for the second array: ");
for(i=0; i<5; i++)
arrTwo[i] = s.nextInt();
System.out.println("\nCommon elements are:");
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(arrOne[i]==arrTwo[j])
System.out.print(arrOne[i]+ " ");
}
}
}
}
14. Write a program to perform addition of two 3*3 matrices.
import java.util.Scanner;
public class addmatrices
{
public static void main()
{
int i, j;
int[][] a = new int[3][3];
int[][] b = new int[3][3];
int[][] c = new int[3][3];
Scanner s = new Scanner(System.in);
System.out.print("Enter 9 elements for first matrix: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.print("Enter 9 elements for second matrix: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
b[i][j] = s.nextInt();
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("\n----Sum of two matrices----");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.print(c[i][j]+ " ");
}
System.out.print("\n");
}
}
}

You might also like