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

Tut5

The document contains three Java programs focused on sorting and processing student data and strings. Exercise1 identifies the top students based on their marks, Exercise2 sorts strings using counting sort, and Exercise3 implements radix sort for strings. Each program utilizes user input to gather data and display sorted results.
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)
2 views

Tut5

The document contains three Java programs focused on sorting and processing student data and strings. Exercise1 identifies the top students based on their marks, Exercise2 sorts strings using counting sort, and Exercise3 implements radix sort for strings. Each program utilizes user input to gather data and display sorted results.
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

Ex1:

package Tut5;

import java.util.Scanner;

public class Exercise1 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("The number of students:");
int n = sc.nextInt();
sc.nextLine();
System.out.println("Enter the numbers of students who have the highest
marks:");
int m = sc.nextInt();
sc.nextLine();
int[] marks = new int[n];
String[] s = new String[n];
for (int i = 0; i < n; i++) {
System.out.print((i + 1) + ".");
s[i] = sc.nextLine();
System.out.print(" Mark:");
marks[i] = sc.nextInt();
sc.nextLine();
System.out.println("");
}
String[] top = findTopStudents(s, marks, m);

System.out.println(m + " students get the highest marks:");


for (int i = 0; i < m; i++) {
System.out.print((i + 1) + ". " + top[m - i - 1]);
System.out.println("");
}
sc.close();
}

public static String[] findTopStudents(String[] names, int[] marks, int m)


{
int n = names.length;
if (m >= n) {
return names;
}

int left = 0;
int right = n - 1;
int k = n - m;

while (left <= right) {


int pivotIndex = partition(names, marks, left, right);
if (pivotIndex < k) {
left = pivotIndex + 1;
} else if (pivotIndex > k) {
right = pivotIndex - 1;
} else {
break;
}
}

String[] topStudents = new String[m];


for (int i = 0; i < m; i++) {
topStudents[i] = names[k + i];
}
return topStudents;
}

private static int partition(String[] names, int[] marks, int low, int
high) {
int pivot = marks[high];
int i = low;

for (int j = low; j < high; j++) {


if (marks[j] <= pivot) {

int tempMark = marks[i];


marks[i] = marks[j];
marks[j] = tempMark;

String tempName = names[i];


names[i] = names[j];
names[j] = tempName;

i++;
}
}

int tempMark = marks[i];


marks[i] = marks[high];
marks[high] = tempMark;

String tempName = names[i];


names[i] = names[high];
names[high] = tempName;

return i;
}
}

Ex2
package Tut5;

import java.util.Scanner;

public class Exercise2 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of strings (n): ");
int n = sc.nextInt();
sc.nextLine();

System.out.print("Enter the length of strings (m): ");


int m = sc.nextInt();
sc.nextLine();

String[] strings = new String[n];


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

System.out.println("Sorted strings:");
for (String str : countSort(strings,m)) {
System.out.println(str);
}
}
static String[] countSort(String[] a, int m) {
int n = a.length;
if (n == 0) return a;
String[] output = new String[n];

for (int j = m - 1; j >= 0; j--) {


int max = 0;
for (String s : a) {
if (j < s.length()) {
max= Math.max(max, (int) s.charAt(j));
}
}
int[] count = new int[max + 1];
for (String s : a) {
if (j < s.length()) {
count[s.charAt(j)]++;
}
}
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
if (j < a[i].length()) {
output[--count[a[i].charAt(j)]] = a[i];
}
}
for (int i = 0; i < n; i++) {
a[i] = output[i];
}
}
return output;
}

}
Ex3
package Tut5;
import java.util.*;
public class Exercise3 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of strings (n): ");


int n = sc.nextInt();
sc.nextLine();

System.out.print("Enter the length of strings (m): ");


int m = sc.nextInt();
sc.nextLine();

String[] strings = new String[n];


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

System.out.println("Sorted strings:");
for (String str : radixSort(strings, m)) {
System.out.println(str);
}
}

static String[] radixSort(String[] a, int m) {


int n = a.length;
for (int j = m - 1; j >= 0; j--) {
countingSort(a, j);
}
return a;
}

static void countingSort(String[] a, int position) {


int n = a.length;
String[] output = new String[n];
int RANGE = 256; // ASCII character range
int[] count = new int[RANGE];

for (String s : a) {
int index = (position < s.length()) ? (int) s.charAt(position)
: 0;
count[index]++;
}

for (int i = 1; i < RANGE; i++) {


count[i] += count[i - 1];
}

for (int i = n - 1; i >= 0; i--) {


int index = (position < a[i].length()) ? (int)
a[i].charAt(position) : 0;
output[--count[index]] = a[i];
}

for (int i = 0; i < n; i++) {


a[i] = output[i];
}
}
}

You might also like