0% found this document useful (0 votes)
21 views3 pages

Experiment 2 Algo

The document describes an experiment implementing the quick sort algorithm. It includes the code to implement quick sort, partitioning an array, and swapping elements. The code takes an integer array as input, applies quick sort, and prints the sorted output array.

Uploaded by

Gaurav Yadav
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)
21 views3 pages

Experiment 2 Algo

The document describes an experiment implementing the quick sort algorithm. It includes the code to implement quick sort, partitioning an array, and swapping elements. The code takes an integer array as input, applies quick sort, and prints the sorted output array.

Uploaded by

Gaurav Yadav
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/ 3

Experiment 2

Student Name: Gaurav UID: 20BCS6806


Branch: AIML Section/Group: 20AIML5-B
Semester: 4th Date of Performance:16/02/22
Subject Name: DESIGN & ANALYSIS OF Subject Code: 20CSP-285
ALGORITHMS LAB

1. Aim/Overview of the practical


Implementation of Quick Sort algorithm.

2. Code:

package com.gaurav;

import java.util.Arrays;
import java.util.Scanner;

public class blackboard {


static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for(int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}

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);
}
}

static void printArray(int[] arr, int size)


{
for(int i = 0; i < size; 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(arr, 0, n - 1);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}

3. Output:
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like