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

Assignment

The document contains two Java programs: the first implements the QuickSort algorithm to sort an array of integers, while the second sorts student details based on CGPA using the Selection Sort algorithm. The QuickSort program allows user input for the array size and elements, and displays the sorted array. The StudentSort program collects student information, sorts it by CGPA, and prints the sorted list of students.

Uploaded by

abhiram.ch.2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment

The document contains two Java programs: the first implements the QuickSort algorithm to sort an array of integers, while the second sorts student details based on CGPA using the Selection Sort algorithm. The QuickSort program allows user input for the array size and elements, and displays the sorted array. The StudentSort program collects student information, sorts it by CGPA, and prints the sorted list of students.

Uploaded by

abhiram.ch.2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME :- ABHIRAM,

REG. NO :- 23BCE7199

Q1.Write a program to implement the quicksort.

import java.util.*;

public class QuickSort{

int arr[];

public void swap(int arr[],int low,int high){

int temp=arr[low];

arr[low]=arr[high];

arr[high]=temp;

public int divide(int arr[],int low,int high){

int pivot=arr[low];

int i=low;

int j=high;

while(i<j){

while(i<high && arr[i]<=pivot){

i++;

while(arr[j]>pivot){

j--;

if(i<j){

swap(arr,i,j);

swap(arr,low,j);
return j;

public void quickSort(int arr[],int low,int high){

if(low<high){

int pivotIndex=divide(arr,low,high);

quickSort(arr,low,pivotIndex-1);

quickSort(arr,pivotIndex+1,high);

public void sort(){

Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of the array");

int n=sc.nextInt();

arr=new int[n];

System.out.println("Enter the "+" "+n+" "+"elments :");

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

arr[i]=sc.nextInt();

quickSort(arr,0,arr.length-1);

public static void main(String args[]){

QuickSort q=new QuickSort();

System.out.println("Array before sorting is:");

System.out.println(Arrays.toString(q.arr));

q.sort();

System.out.println("The sorted array is :");

System.out.println(Arrays.toString(q.arr));

}}
Q2. 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;

String rollNumber;

String address;

double cgpa;

// Constructor to initialize a Student object

public Student(String name, String rollNumber, String address, double cgpa) {

this.name = name;

this.rollNumber = rollNumber;

this.address = address;

this.cgpa = cgpa;
}

// Method to display student details

public void display() {

System.out.println("Name: " + name + ", Roll Number: " + rollNumber + ", Address: " + address + ",
CGPA: " + cgpa);

public class StudentSort {

// Method for selection sort based on CGPA

public static void selectionSort(Student[] students) {

int n = students.length;

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;

// Swap the found minimum element with the first element

Student temp = students[minIndex];

students[minIndex] = students[i];

students[i] = temp;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


// Taking the number of students

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

int n = scanner.nextInt();

scanner.nextLine(); // Consume the newline

Student[] students = new Student[n];

// Input student details

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

System.out.println("Enter details for student " + (i + 1) + ":");

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

String name = scanner.nextLine();

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

String rollNumber = scanner.nextLine();

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

String address = scanner.nextLine();

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

double cgpa = scanner.nextDouble();

scanner.nextLine(); // Consume the newline

students[i] = new Student(name, rollNumber, address, cgpa);

// Sort students by CGPA using selection sort

selectionSort(students);

// Display sorted students

System.out.println("\nStudents sorted by CGPA in non-decreasing order:");

for (Student student : students) {


student.display();

scanner.close();

You might also like