0% found this document useful (0 votes)
22 views5 pages

22K-4818 - Lab 3

This document contains 4 questions that provide code examples of sorting and searching algorithms in Java, including: 1) A selection sort method to sort an integer array and output the first 4 sorted elements. 2) An insertion sort method that sorts an integer array and moves the largest element to the middle. 3) A selection sort method that sorts a string array alphabetically. 4) Code that demonstrates binary and linear searching on a string array, outputting the number of steps to find present and absent elements.

Uploaded by

k224818
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)
22 views5 pages

22K-4818 - Lab 3

This document contains 4 questions that provide code examples of sorting and searching algorithms in Java, including: 1) A selection sort method to sort an integer array and output the first 4 sorted elements. 2) An insertion sort method that sorts an integer array and moves the largest element to the middle. 3) A selection sort method that sorts a string array alphabetically. 4) Code that demonstrates binary and linear searching on a string array, outputting the number of steps to find present and absent elements.

Uploaded by

k224818
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/ 5

DS-LAB 3

22K-4818

Q1.
import java.util.Scanner;

public class Task1 {


public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}

public static void main(String a[]){


int arr[] = new int[10];
Scanner inp = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("Enter Element " + i + " Of Array :");
arr[i] = inp.nextInt();
}
System.out.println("Before Selection Sort");
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();

selectionSort(arr);

System.out.println("After Selection Sort - 4 Min Elements");


for (int i = 0; i < 4; i++) {
System.out.print(arr[i]+ " ");
}
}
}
Q2.
public class Task2 {
public static void InsertionSort(int arr[]) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i -1;

while (j >= 0 && arr[j]>key) {


arr[j+1] = arr[j];
j = j - 1;
}
arr[j+1] = key;
}
int temp = arr[arr.length-1]; // max //9
for (int i = arr.length - 2 ; i > arr.length/2 ; i--) {
int num = arr[i];
arr[i+1] = num;
}
arr[(arr.length)/2] = temp;
}

public static void main(String[] args) {


int arr[] = {20,12,15,2,10,1,13,9,5};
System.out.println("Array Before: ");
for (int i :
arr) {
System.out.print(i + " ");
}

System.out.println();
InsertionSort(arr);
System.out.println("Array After: ");
for (int i :
arr) {
System.out.print(i + " ");
}
}
}

Q3.
public class Task3 {
public static void selectionSort(String[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j].compareToIgnoreCase(arr[index])<0){
index = j;
}
}
String smallerString = String.valueOf(arr[index]);
arr[index] = arr[i];
arr[i] = smallerString;
}
}

public static void main(String[] args) {


String arr[] = {"Hani","Laiba","Umair","Hafsa"};
System.out.println("Array Before: ");
for (String i :
arr) {
System.out.print(i + " ");
}
System.out.println();

selectionSort(arr);
System.out.println("Array After: ");
for (String i :
arr) {
System.out.print(i + " ");
}
}
}

Q4.
public class Task4 {
public static void binarySearch(String arr[], String key) {
int steps = 1;
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key.equalsIgnoreCase(arr[mid])) {
System.out.println("Step Count For " + key + " in Binary
Search : " + steps);
return;
} else if (key.compareToIgnoreCase(arr[mid]) < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
steps++;
}
System.out.println("No Element Named : " + key + " Found in Binary
Search");
}

public static void linearSearch(String arr[], String key) {


for (int i = 0; i < arr.length; i++) {
if (arr[i].equalsIgnoreCase(key)) {
System.out.println("Step Count For " + key + " in Linear
Search : " + (i + 1));
return;
}
}
System.out.println("No Element Named : " + key + " Found in Linear
Search");
}
public static void main(String[] args) {
String arr[] = {"Ahmed", "Ali", "Basit", "Karim", "Rizwan", "Sarwar",
"Tariq", "Taufiq", "Yasin", "Zulfiqar"};

binarySearch(arr, "Aftab");
linearSearch(arr, "Aftab");

binarySearch(arr, "Tariq");
linearSearch(arr, "Tariq");

binarySearch(arr, "Rizwan");
linearSearch(arr, "Rizwan");
}
}

You might also like