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

Implement the Selection Sort algorithm by taking single character input (10 lett

ID: 3859129 • Letter: I

Question

Implement the Selection Sort algorithm by taking single character input (10 letters) from the user. Perform the bubble sort on 10 integers input by the user in a descending order (bigger to smaller). Write a method that accepts an integer array as a parameter and sorts it using the insertion sort algorithm. Write a method that accepts an integer and an array of integers, and returns true if the integer is contained in the array. You may assume and an array of integers, and returns true if the integer is contained in the array. You may assume that the array is sorted, and your method should use a binary search. You can use recursive or iterative approach to solve the problem.

Explanation / Answer

Q1)

SelectionSortLetters.java

import java.util.Scanner;

public class SelectionSortLetters {

public static void main(String[] args) {
// Creating an Character Array of size 10
char arr[] = new char[10];

// Creating an Char array reference
char sorted[] = null;

// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

// Getting the 10 characters entered by the user and populate them into
// an array
System.out.println("Enter 10 Characters(seperated by space) :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.next(".").charAt(0);
}

// Calling the method by passing the array as argument
sorted = selectionSort(arr);

// Displaying the char array after sorting
System.out.println("After Sorting :");
for (int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}

}

/*
* This method is used to sort the character array based on selection sort
* algorithm
*/
private static char[] selectionSort(char[] beforeSort) {

for (int i = 0; i < beforeSort.length; i++) {
int m = i;
for (int j = i + 1; j < beforeSort.length; j++) {
if (beforeSort[j] < beforeSort[m])
m = j;
}
char small = beforeSort[m];
beforeSort[m] = beforeSort[i];
beforeSort[i] = small;
}
return beforeSort;
}

}

_________________________

Output:

Enter 10 Characters(seperated by space) :
t r f a c v b u i k
After Sorting :
a b c f i k r t u v

__________________

Q2)

BubbleSortNumbers.java

import java.util.Scanner;

public class BubbleSortNumbers {

public static void main(String[] args) {
// Creating an integer Array of size 10
int arr[] = new int[10];

// Creating an Char array reference
int sorted[] = null;

// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

// Getting the 10 numbers entered by the user and populate them into
// an array
System.out.println("Enter 10 numbers(seperated by space) :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}

// Calling the method by passing the array as argument
sorted = bubbleSort(arr);

// Displaying the Integer array after sorting
System.out.println("After Sorting :");
for (int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}

}

//This Method Logic will Sort the Array of elements in Descending order
private static int[] bubbleSort(int[] array) {

int temp1;
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array[i] < array[j]) {
temp1 = array[i];
array[i] = array[j];
array[j] = temp1;
}
}
}
return array;
}

}

___________________________

Output:

Enter 10 numbers(seperated by space) :
34 54 12 33 89 90 48 19 9 50
After Sorting :
90 89 54 50 48 34 33 19 12 9

________________________

Q3)

InsertionSort.java

import java.util.Scanner;

public class InsertionSort {

public static void main(String[] args) {
// Creating an integer Array of size 10
int arr[] = new int[10];

// Creating an Char array reference
int sorted[] = null;

// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

// Getting the 10 numbers entered by the user and populate them into
// an array
System.out.println("Enter 10 numbers(seperated by space) :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}

// Calling the method by passing the array as argument
sorted = InsertionSort(arr);

// Displaying the Integer array after sorting
System.out.println("After Sorting :");
for (int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}

}

//Method which sorts the integer array using insertion sort
private static int[] InsertionSort(int[] beforeSort) {
int len = beforeSort.length;
for (int k = 1; k < len; k++) {
int z = beforeSort[k];
int l = k - 1;
while ((l > -1) && (beforeSort[l] > z)) {
beforeSort[l + 1] = beforeSort[l];
l--;
}
beforeSort[l + 1] = z;

}
return beforeSort;
}
}

________________________

Output:

Enter 10 numbers(seperated by space) :
34 56 12 9 87 71 27 65 11 33
After Sorting :
9 11 12 27 33 34 56 65 71 87

__________________

Q4)

BinarySearch.java

public class BinarySearch {

public static void main(String[] args) {
// Declaring variables
int num;

// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

// Declaring one dimensional array
int arr[] = {4, 5, 6, 7, 12, 23, 34, 56, 78, 90, 123, 124, 156, 178, 199, 200, 230, 245, 250, 255};

// getting the number entered by the user
System.out.print("Enter the number to search :");
num = sc.nextInt();

// calling the method by passing the array and user entered number as
// arguments
int pos = searchForElement(arr, num);

// Displaying the position of the number in the array if found
if (pos != -1)
System.out.println("The " + num + " is found at Position " + pos);
else
System.out.println("** Number not in array **");
}

/*
* method implementation which search for the number in the array using
* binary search algorithm
*
* @Params array and number
*
* @return Position
*/
private static int searchForElement(int[] arr, int num) {
int first = 0, mid = 0;
int last = arr.length - 1;
while (first <= last) {
mid = (first + last) / 2;
if (num == arr[mid])
return mid;
if (num < arr[mid])
last = mid - 1;
else
first = mid + 1;
}
return -1;
}

}

______________________

Output:

Enter the number to search :250
The 250 is found at Position 18

______________________

Output#1:

Enter the number to search :23
The 23 is found at Position 5

______________________Thank You