Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class binary_sort { public static void sort(int a[],int n){ for (int i=0;

ID: 3783050 • Letter: P

Question

public class binary_sort {
   public static void sort(int a[],int n){
for (int i=0;i<n;++i){
int temp=a[i];
int left=0;
int right=i;
while (left<right){
int middle=(left+right)/2;
if (temp>=a[middle])
left=middle+1;
else
right=middle;
}
for (int j=i;j>left;--j){
swap(a,j-1,j);
}
}
}

public static void main(String[] args){
  
int a[]=new int[]{12,10,34,23,9,7,8,5,6};
sort(a,a.length);
for (int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
public static void swap(int a[],int i,int j){
int k=a[i];
a[i]=a[j];
a[j]=k;
}
}

Add a random number generator that writes 10 random numbers, valued between 1 and 40, to the array "a"- and then sorts using the sort method (Using the code provided, in Java).

Explanation / Answer

//random number generation of 10 numbers and stored in a[] is added in above code. See code below

import java.util.Random; //Random class required to generate random number

public class binary_sort {
    public static void sort(int a[],int n){
        for (int i=0;i<n;++i){
            int temp=a[i];
            int left=0;
            int right=i;
            while (left<right){
                int middle=(left+right)/2;
                if (temp>=a[middle])
                    left=middle+1;
                else
                    right=middle;
            }
            for (int j=i;j>left;--j){
                swap(a,j-1,j);
            }
        }
    }

    public static void main(String[] args){
      
        int a[]=new int[10];
        Random rn = new Random(); //object rn is created
        for(int i=0;i<10;i++)
        {
            a[i]=rn.nextInt(40)+1; //random number generation between 1 to 40
        }

        sort(a,a.length);
        for (int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
    public static void swap(int a[],int i,int j){
        int k=a[i];
        a[i]=a[j];
        a[j]=k;
    }
}

/* Output

1                                                                                                                                                                        8                                                                                                                                                                        14                                                                                                                                                                       15                                                                                                                                                                       19                                                                                                                                                                       29                                                                                                                                                                       31                                                                                                                                                                       32                                                                                                                                                                       34                                                                                                                                                                       37 */