0% found this document useful (0 votes)
36 views9 pages

DSA Assignment-4

Uploaded by

aryan.23bce8252
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)
36 views9 pages

DSA Assignment-4

Uploaded by

aryan.23bce8252
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/ 9

DSA Assignment - 4Mod

Thopireddy Aryan Reddy


23BCE8252

1Q. Write a program to implement the quicksort.


public class QuickSort {

public static void quickSort(int[] arr, int low, int


high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

public static int partition(int[] arr, int low, int high)


{

int pivot = arr[high];

int i = (low - 1);

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

if (arr[j] < pivot) {

i++;
int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return i + 1;

public static void printArray(int[] arr) {

for (int i : arr) {

System.out.print(i + " ");

System.out.println();

public static void main(String[] args) {

int[] arr = {10, 7, 8, 9, 1, 5};

int n = arr.length;

System.out.println("Original array:");

printArray(arr);
quickSort(arr, 0, n - 1);

System.out.println("Sorted array:");

printArray(arr);

2Q.Write a program that takes the details of


Students (name, roll number, address, CGPA)
and sort it in a non-decreasing order
using Selection sort based on CGPA.

import java.util.Scanner;

class Student {

String name;

int rollNumber;

String address;

double cgpa;

Student(String name, int rollNumber, String address,


double cgpa) {

this.name = name;

this.rollNumber = rollNumber;

this.address = address;

this.cgpa = cgpa;

}
public String toString() {

return "Name: " + name + ", Roll No: " + rollNumber +


", Address: " + address + ", CGPA: " + cgpa;

public class StudentSort {

public static void selectionSort(Student[] students, int


n) {

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

int minIndex = i;

for (int j = i + 1; j < n; j++) {

if (students[j].cgpa <
students[minIndex].cgpa) {

minIndex = j;

Student temp = students[minIndex];

students[minIndex] = students[i];

students[i] = temp;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


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

int n = scanner.nextInt();

Student[] students = new Student[n];

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

System.out.println("Enter details for student " +


(i + 1) + ":");

System.out.print("Name: ");

String name = scanner.next();

System.out.print("Roll Number: ");

int rollNumber = scanner.nextInt();

System.out.print("Address: ");

String address = scanner.next();

System.out.print("CGPA: ");

double cgpa = scanner.nextDouble();

students[i] = new Student(name, rollNumber,


address, cgpa);

selectionSort(students, n);

System.out.println("\nStudents sorted by CGPA in non-


decreasing order:");

for (Student student : students) {

System.out.println(student);

}
scanner.close();

1.cQ Write a Program to read N individual


characters and display them in alphabetical
order using merge sort.
import java.util.Scanner;

public class MergeSortCharacters {

public static void mergeSort(char[] arr, int left, int


right) {

if (left < right) {

int mid = (left + right) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

public static void merge(char[] arr, int left, int mid,


int right) {

int n1 = mid - left + 1;

int n2 = right - mid;

char[] L = new char[n1];

char[] R = new char[n2];


for (int i = 0; i < n1; i++)

L[i] = arr[left + i];

for (int j = 0; j < n2; j++)

R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];
j++;

k++;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int n = scanner.nextInt();

char[] characters = new char[n];

System.out.println("Please enter " + n + " individual


characters:");

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

characters[i] = scanner.next().charAt(0);

mergeSort(characters, 0, n - 1);

System.out.println("Characters in alphabetical
order:");

for (char c : characters) {

System.out.print(c + " ");

scanner.close();

You might also like