Write a function that will sort an array of strings using se

Write a function that will sort an array of strings using selection sort Write a function that will perform a binary search on an array of strings Write a driver program that will: read a list of names from \"names.txt\" (one name per line) sort the names display the names in sorted order allow the user to type in a name display where in the array the user typed name is (-1 if it is not in the array)

Solution

1. selection sorting:

public class SelectionSort {
public static void selectionsorting(int[] array){
for (int i = 0; i < arr.length - 1; i++)
{
int base = i;
for (int j = i + 1; j < array.length; j++){
if (array[j] < array[base]){
base = j;
}
}
int smallnum = array[base];   
array[base] = array[i];
array[i] = smallnum;
}
}

public static void main(String a[]){
int[] array1 = {2,1,4,5,14,125,57,22};
System.out.println(\"Before Selection Sorting\");
for(int i:array1){
System.out.print(i+\" \");
}
System.out.println();
  
selectionSort(array1);

System.out.println(\"After Selection Sorting\");
for(int i:arr1){
System.out.print(i+\" \");
}
}
}

2. binary search

class BSearch
{
public static void main(String args[])
{
int a, b_first, b_last, b_middle, n, search, array[];

Scanner in = new Scanner(System.in);
System.out.println(\"Enter numbers\");
n = in.nextInt();
array = new int[n];

System.out.println(\"Enter \" + n + \" integers\");


for (a = 0; a < n; a++)
array[a] = in.nextInt();

System.out.println(\"Enter value to find\");
search = in.nextInt();

b_first = 0;
b_last = n - 1;
b_middle = (b_first + b_last)/2;

while( b_first <= b_last )
{
if ( array[b_middle] < search )
b_first = b_middle + 1;
else if ( array[b_middle] == search )
{
System.out.println(search + \" found @ \" + (b_middle + 1) + \".\");
break;
}
else
b_last = b_middle - 1;

b_middle = (b_first + b_last)/2;
}
if ( b_first > b_last )
System.out.println(search + \" is not .\ \");
}
}

 Write a function that will sort an array of strings using selection sort Write a function that will perform a binary search on an array of strings Write a driv
 Write a function that will sort an array of strings using selection sort Write a function that will perform a binary search on an array of strings Write a driv

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site