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

OOP Sorting Algorithms in Java

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)
59 views5 pages

OOP Sorting Algorithms in Java

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

Question 1

Question 2
import [Link];

public class Numbers {


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

// Read the number of elements


[Link]("Enter the number of integers: ");
int n = [Link]();
int[] numbers = new int[n];

// Read the elements


[Link]("Enter the integers:");
for (int i = 0; i < n; i++) {
numbers[i] = [Link]();
}

// Sort the array using selection sort


selectionSort(numbers);
// Print the sorted array
[Link]("Sorted integers:");
for (int number : numbers) {
[Link](number + " ");
}
}

// Selection Sort Algorithm


public static void selectionSort(int[] arr) {
int n = [Link];
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 [Link];

public class Strings {


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

// Read the number of elements


[Link]("Enter the number of strings: ");
int n = [Link]();
[Link](); // Consume newline

String[] strings = new String[n];

// Read the elements


[Link]("Enter the strings:");
for (int i = 0; i < n; i++) {
strings[i] = [Link]();
}

// Sort the array using selection sort


selectionSort(strings);

// Print the sorted array


[Link]("Sorted strings:");
for (String str : strings) {
[Link](str + " ");
}
}

// Selection Sort Algorithm


public static void selectionSort(String[] arr) {
int n = [Link];
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 [Link];

public class Numbers {


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

// Read the number of elements


[Link]("Enter the number of integers: ");
int n = [Link]();
int[] numbers = new int[n];

// Read the elements


[Link]("Enter the integers:");
for (int i = 0; i < n; i++) {
numbers[i] = [Link]();
}

// Sort the array using insertion sort in descending order


insertionSort(numbers);

// Print the sorted array


[Link]("Sorted integers in descending order:");
for (int number : numbers) {
[Link](number + " ");
}
}

// Insertion Sort Algorithm (Descending Order)


public static void insertionSort(int[] arr) {
int n = [Link];
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