Sorting

Sorting is a process of ordering or placing a list of elements from a collection in some kind of order. It is nothing but storage of data in sorted order. Sorting can be done in ascending and descending order. It arranges the data in a sequence which makes searching easier.

There are various sorting techniques – quick sort, merge sort, bubble sort etc.

But we will work on bubble sort for now.

Bubble sort –Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed.in simple words, in bubble sort largest element shifted into last-end position.

Let’s make some code of sorting (bubble sort).

  1. Write a code, which will convert an unsorted array into sorted array (ascending).
public class code2 {
    public static void main(String[] args) {
        // array ‘arr’ size = 6
intarr[]={10,-2,34,6,90,5};
int j=0;
System.out.println(“before sort array elements”);
        for (j=0;j<6;j++) {
System.out.println(arr[j]);
        }
 
        // bubble sort
        for(int x=0;x<6;x++){
            for (inti = 0; i<5; i++) {
                if (arr[i]>arr[i+1]) {
int temp = arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
                }
            }
        }
        //displays all array elements
System.out.println(“after sort array elements”);
        for (j=0;j<6;j++) {
System.out.println(arr[j]);
        }
    }
}
output

2. Write a code, which will convert an unsorted array into descending sorted array.

   import java.util.Scanner;
 
class q6{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
intn,i,j,temp=0;
System.out.println(“enter array size: “);
        n=sc.nextInt();
int array[]=new int[n];
        for(i=0;i<n;i++){
System.out.println(“enter element: “);
            array[i]=sc.nextInt();
        }
        for(j=0;j<n;j++){
 
        for(i=0;i<n-1;i++){
            if(array[i]>array[i+1]){
                temp=array[i];
                array[i]=array[i+1];
                array[i+1]=temp;
            }
        }
 
        }
System.out.println(“\n sort array (assending):”);
        for(i=0;i<n;i++){
System.out.println(array[i]);
        }
int p=0,q=n-1;
int loop = n/2;
    for(i=0;i<loop;i++){
        temp=array[p];
        array[p]=array[q];
        array[q]=temp;
        q–;
        p++;
    }
System.out.println(“\n array after 2nd sort(descending) “);
        for(i=0;i<n;i++){
System.out.println(array[i]);
        }
 
    }
}
output

3. Construct a number using array integer elements.

// converts an array elements into integer number
class q5{
                    public static void main(String[] args) {
                                        int array[] = new int[5];
                                        array[0]=2;
                        array[1]=1;
                                        array[2]=3;
                                        array[3]=6;
                                        array[4]=9;
                                        intx,y; String Snum=””,num;
                                        for (x=0;x<5 ;x++ ) {
                                                            y = array[x];
                                                            num = Integer.toString(y); // converts Strings into integer
                                                            Snum = Snum + num;
                                        }
                                        intfinalNum = Integer.parseInt(Snum); // converting String variable into Integer
                                        System.out.println(“\n number is “+finalNum);
                                                           
                    }
}
output

Searching

Generally in array, we either do linear or binary search. But we will discuss and so coding with linear search.

Linear search –here we checks each element of array one by one. If the desired found, then execution get stopped. This search also known as sequential search.

Let’s make some codes of searching(linear search).

  1. Write a code, to search an element in array.
   import java.util.Scanner;
 
class code1{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
int array[] = {10,20,-31,45,65};
System.out.println(“enter your search element: “);
int n=sc.nextInt();
 
         for(int x=0;x<5;x++){
          if (n==array[x]) {
System.out.println(“element found!!”);
          }
        }
 
    }
}
output

Let’s make another difficult programs using sorting.

2. Write a code, to find the smallest element of array.

  import java.util.Scanner;
 
class q2{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
inti,n; // n  = array-size/length
System.out.println(“\n Enter array size : “);
        n = sc.nextInt();
int array[] = new int[n];
        for(i=0;i<n;i++) // array initialization
        {
System.out.println(“\n enter element : “);
            array[i] = sc.nextInt();
        }
intlooptime = n-1;
int x;
        for(x=0;x<looptime;x++){
 
            if(array[x+1]<array[0]){
                array[0] = array[x+1];
            }
 
        }
System.out.println(“\n smallest element is ” + array[0]);
    }
}
output

3. Find the largest element of array.

import java.util.Scanner;
 
class q3{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
inti,n; // n  = array-size/length
System.out.println(“\n Enter array size : “);
        n = sc.nextInt();
int array[] = new int[n];
        for(i=0;i<n;i++){
System.out.println(“\n enter element : “);
            array[i] = sc.nextInt();
        }
intlooptime = n-1;
int x;
 
        for(x=0;x<looptime;x++){
 
            if(array[x+1]>array[0]){
                array[0] = array[x+1];
            }
 
        }
System.out.println(“\n largest  element is ” + array[0]);
 
    }
}
output

4. Write a code, which print second largest number of an array.

import java.util.Scanner;
 
class q4{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
inti,n; // n  = array-size/length
System.out.println(“\n Enter array size : “);
        n = sc.nextInt();
int array[] = new int[n];
        for(i=0;i<n;i++){
System.out.println(“\n enter element : “);
            array[i] = sc.nextInt();
        }
intlooptime = n-1;
intx,temp=0;
        for(x=0;x<looptime;x++){
            if(array[x+1]>array[0]){
                temp = array[0]; // storing index 0 value into ‘temp’ variable
                array[0] = array[x+1];
                array[x+1] = temp;
            }
 
        }
System.out.println(“\n 1st largest  element is ” + array[0]);
 
 
        for(x=0;x<n-1;x++){
 
           if(array[0]==array[x+1]){
            array[x] = 0;
           }
 
        }
 
 
         for(x=1;x<looptime;x++){
          if(array[x+1]>array[1]){
                array[1] = array[x+1];
            }
 
        }
 
System.out.println(“\n 2nd largest  element is ” + array[1]);
    }
}
output

Array Merge

Here we merges two 1D arrays. We can merge them on many basics. Let’s understand some examples of it below.

Merge two unsorted arrays into another new array.

import java.util.Scanner;
 
class merge1{
                    public static void main(String[] args) {
                                        Scanner sc = new Scanner(System.in);
                                        intn,m,i,p,q,j,lastIn=0;
                                        System.out.println(“\n enter first array size: “);
                                        n=sc.nextInt();
                                        int array1[]=new int[n];
                                       
                                        for (i=0;i<n ;i++ ) {
                                                            System.out.println(” enter element : “);
                                                            array1[i] = sc.nextInt();
                                        }
                                        System.out.println(“\n enter second array size: “);
                                        m=sc.nextInt();
                                       
                    int array2[]=new int[m];
                    for (i=0;i<m ;i++ ) {
                                                            System.out.println(“enter element : “);
                                                            array2[i] = sc.nextInt();
                                        }
                                        p=n+m;
                                        intfarr[]=new int[p];
                                       
                                                            lastIn=n;
                                                            for (i=0;i<n ;i++ ) {
                                                                                farr[i]=array1[i];
                                                            }
                                                            for (i=0;i<m ;i++ ) {
                                                                                farr[lastIn]=array2[i];
                                                                                lastIn++;
                                                            }
                                                            System.out.println(“\n final array is”);
                                                            for (i=0;i<n+m ;i++ ) {
                                                            System.out.println(“\t”+farr[i]);
                                        }
                                       
                    }
}
output

Merge two unsorted arrays into another sorted array.

import java.util.Scanner;
 
class merge2{
                    public static void main(String[] args) {
                                        Scanner sc = new Scanner(System.in);
                                        intn,m,i,p,q,j,temp;
                                        System.out.println(” enter first array size: “);
                                        n=sc.nextInt();
                                        int array1[]=new int[n];
                                       
                                        for (i=0;i<n ;i++ ) {
                                                            System.out.println(” enter element : “);
                                                            array1[i] = sc.nextInt();
                                        }
                                        System.out.println(” enter second array size: “);
                                        m=sc.nextInt();
                                       
                    int array2[]=new int[m];
                    for (i=0;i<m ;i++ ) {
                                                            System.out.println(“enter element : “);
                                                            array2[i] = sc.nextInt();
                                        }
                                        System.out.println(” before sorting array 1 : “);
                                        for (i=0;i<n ;i++ ) {
                                                            System.out.println(“\t”+array1[i]);
                                        }
                                        System.out.println(” before sorting array 2 : “);
                                        for (i=0;i<m ;i++ ) {
                                                            System.out.println(“\t”+array2[i]);
                                        }
                                        p=n+m;
                                        intfarr[]=new int[p];
                                                            intlastIn;
                                                            lastIn=n;
                                                            for (i=0;i<n ;i++ ) {
                                                                                farr[i]=array1[i];
                                                            }
                                                            for (i=0;i<m ;i++ ) {
                                                                                farr[lastIn]=array2[i];
                                                                                lastIn++;
                                                            }
                                                            System.out.println(“final array before sort is”);
                                                            for (i=0;i<n+m ;i++ ) {
                                                            System.out.println(“\t”+farr[i]);
                                        }
                                                            q=p-1;
                                                           
                                                            for (j=0;j<p ;j++ ) {
                                                                                for (i=0;i<q ; i++) {
                                                                                                    if (farr[i]>farr[i+1]) {
                                                                                                                        temp=farr[i];
                                                                                                                        farr[i]=farr[i+1];
                                                                                                                        farr[i+1]=temp;
                                                                                                    }
                                                                                }
                                                            }
                                                            System.out.println(“final array after sort is”);
                                                            for (i=0;i<n+m ;i++ ) {
                                                            System.out.println(“\t”+farr[i]);
                                        }
                                       
                    }
}
output