0% found this document useful (0 votes)
18 views

OOP Tutorial

Java code about arrays

Uploaded by

Devlyn Tagoe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

OOP Tutorial

Java code about arrays

Uploaded by

Devlyn Tagoe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Question 1

Question 2
import java.util.Scanner;

public class Numbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read the number of elements


System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();
int[] numbers = new int[n];

// Read the elements


System.out.println("Enter the integers:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}

// Sort the array using selection sort


selectionSort(numbers);
// Print the sorted array
System.out.println("Sorted integers:");
for (int number : numbers) {
System.out.print(number + " ");
}
}

// Selection Sort Algorithm


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

Question 3
import java.util.Scanner;

public class Strings {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read the number of elements


System.out.print("Enter the number of strings: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume newline

String[] strings = new String[n];

// Read the elements


System.out.println("Enter the strings:");
for (int i = 0; i < n; i++) {
strings[i] = scanner.nextLine();
}

// Sort the array using selection sort


selectionSort(strings);

// Print the sorted array


System.out.println("Sorted strings:");
for (String str : strings) {
System.out.print(str + " ");
}
}

// Selection Sort Algorithm


public static void selectionSort(String[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j].compareTo(arr[minIndex]) < 0) {
minIndex = j;
}
}
String temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
Question 4
import java.util.Scanner;

public class Numbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read the number of elements


System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();
int[] numbers = new int[n];

// Read the elements


System.out.println("Enter the integers:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}

// Sort the array using insertion sort in descending order


insertionSort(numbers);

// Print the sorted array


System.out.println("Sorted integers in descending order:");
for (int number : numbers) {
System.out.print(number + " ");
}
}

// Insertion Sort Algorithm (Descending Order)


public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

// Move elements of arr[0..i-1] that are less than key to one


position ahead
// of their current position
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
}

You might also like