The document discusses searching and sorting techniques. It defines searching as finding an item that meets a criterion, and sorting as rearranging items in order. The types of searching are linear and binary search, and sorting types include insertion, merge, bubble, quick, counting, and shell sort. Quicksort and selection sort algorithms are implemented via code examples to sort arrays of integers and student data by CGPA respectively.
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 ratings0% found this document useful (0 votes)
61 views6 pages
Assignment 6 Dsa
The document discusses searching and sorting techniques. It defines searching as finding an item that meets a criterion, and sorting as rearranging items in order. The types of searching are linear and binary search, and sorting types include insertion, merge, bubble, quick, counting, and shell sort. Quicksort and selection sort algorithms are implemented via code examples to sort arrays of integers and student data by CGPA respectively.
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/ 6
ASSIGNMENT – 6
QUESTIONS :-
1) WHAT IS SORTING AND WHAT IS SEARCHING?
2) LIST DIFFERENT TYPES OF SEARCHING AND SORTING
TECHNIQUES?
3) WRITE A PROGRAM TO IMPLEMENT QUICK SORT?
4) WRITE A PROGRAM THAT TAKES DETAILS OF STUDENT (NAME,
ROLL N0, ADDRESS, CGPA) AND SORT IT IN NON-DECREASING ORDER USING SELECTION SORT BASED ON THE CGPA?
SOLUTIONS :-
1) Searching here refers to finding an item in the
array that meets some specified criterion. Sorting refers to rearranging all the items in the array into increasing or decreasing order (where the meaning of increasing and decreasing can depend on the context).
2) The different types of searching and sorting
techniques are: - a) Searching- Linear search and Binary search b) Sorting- Insertion, Merge, Bubble, Quick, Counting, Shell and Cycle short etc. 3) // Java program for implementation of Quick Sort class Quick Sort { int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low-1); // index of smaller element for (int j=low; j<high; j++) { if (arr[j] <= pivot) { i++; // swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // swap arr[i+1] and arr[high] (or pivot) int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; return i+1; } void sort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); sort(arr, low, pi-1); sort(arr, pi+1, high); } } static void printArray(int arr[]) { int n = arr.length; for (int i=0; i<n; ++i) System.out.print(arr[i]+" "); System.out.println(); } public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5}; int n = arr.length; QuickSort ob = new QuickSort(); ob.sort(arr, 0, n-1); System.out.println("sorted array"); printArray(arr); } } 4 ) import java.util.Scanner; class Student { String name; int rollNo; String address; double cgpa; public Student(String name, int rollNo, String address, double cgpa) { this.name = name; this.rollNo = rollNo; this.address = address; this.cgpa = cgpa; } public String toString() { return "Name: " + name + ", Roll No: " + rollNo + ", Address: " + address + ", CGPA: " + cgpa; } } public class StudentSort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of students: "); int n = scanner.nextInt(); scanner.nextLine(); // Consume the newline character 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.nextLine(); System.out.print("Roll No: "); int rollNo = scanner.nextInt(); scanner.nextLine(); // Consume the newline 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, rollNo, address, cgpa); } 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[i]; students[i] = students[minIndex]; students[minIndex] = temp; } System.out.println("Students sorted in non-decreasing order based on CGPA:"); for (Student student : students) { System.out.println(student); } scanner.closeVC(); } }