From 0bb9173ed162a65b23c283a17e3bf14e56e62a97 Mon Sep 17 00:00:00 2001 From: Sandip Kumar Dey <96774612+sandipkumardey@users.noreply.github.com> Date: Sat, 6 Apr 2024 17:47:44 +0530 Subject: [PATCH] Add files via upload --- QuickSort.class | Bin 0 -> 1357 bytes QuickSort.java | 73 ++++++++++++++++++++++++++++++++++ RemoveDuplicateElements.class | Bin 0 -> 1711 bytes RemoveDuplicateElements.java | 60 ++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 QuickSort.class create mode 100644 QuickSort.java create mode 100644 RemoveDuplicateElements.class create mode 100644 RemoveDuplicateElements.java diff --git a/QuickSort.class b/QuickSort.class new file mode 100644 index 0000000000000000000000000000000000000000..50ba7f875492d42612cf028add8472d66c1d0793 GIT binary patch literal 1357 zcmaJ=U2oG?7=BLT#EI*+rjUNcVJ%-voh{i0E3mLiED|D8!Jr^es5Z?_EHFvzEOwZB zz5a+@?5a1r+0AwV6`J}3_#NErFCfA896MTzS|Q8leSQ3XJkN8^`}+N>0{}O1LqiH_ z6^f1wvI534=ebksJN}cJz5UGXL;~3xp6^8)0_pPFLk&6PRcJb6^s~{ z|LM?I%UjK6bM0XsGniFj=s1PB5309z!pI%4XfS4OVKeUQ1+{x4&yQNs$aMzwIYK<6 zqkwsVg@My^8-d?(qDNlzlwKG)eiSmd++0i2W7y2~{3mq{i&#>ztfL51;PeUg1u{d~ zO_n%bAmMse$0t}}WH{`5jGPc_N*&JWI4?y|!k?WXO(W>K0<)W*@7^5`w%ySKXS>fG zW#D+eziN%uo?A;)O7y(tbfRFiBk=1{876^A z@!O8y?YrTH%^>KFhxOw;j_iDp_+V$~o)CUiiR7NukDlSAM8oO#TVCYW1@xxxyQ46$*^h_viYL6V!c$H0(t*Pu-z=ydy%MdEJJw5xKk2YNSNU7| zd0fJ$v@-NJC`i-(U}%49?X)pvx0S5j&KSDg&KmdawrZ5^cFuTgw~egAMKy9wXcOPo{TeEVohlRf|T`{Y#a2j}lh250+{oj8ihE$AUnJB=H*Vy+aAvgK` zi1vF@Df5qx)H^g(tckZ+B2td+1nFI7D2yl-%{?qzMROme4Xap+n_DI2M{cNxjpk!b zrnu^Srg4U&ZNi9~q#;aba#AL>e<31?>$+uDH;PJkT`8_-3K=#stwN?++QZDQBCLW^ z$n0ZvH$zTu_!akDmG%!NCj>7MG)P`y$2_m|9Oki%1)j+w1t_4x0lz{d3EA{}PNWcv zZ;4Zr<2eCzc|vy9DppC7ux!gL*@tI27i+P?b(M+dNbWp2RALyP;d9y?OMSt&igkWz L^seGM>R9>@0KFYz literal 0 HcmV?d00001 diff --git a/QuickSort.java b/QuickSort.java new file mode 100644 index 0000000..be4fb24 --- /dev/null +++ b/QuickSort.java @@ -0,0 +1,73 @@ +class QuickSort { + public static void main(String[] args) { + int[] arr = {11, 3, 12, 81, 40, 90, 1, 8, 4, 55}; + quickSort(arr, 0, arr.length - 1); + for (int i = 0; i < arr.length; i++) { + System.out.print(arr[i] + " "); + } + } + + public static void quickSort(int[] arr, int low, int high) { + if (low < high) { + int pi = split(arr, low, high); + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } + } + + private static int split(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++; + swap(arr, i, j); + } + } + swap(arr, i + 1, high); + return i + 1; + } + + private static void swap(int[] arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +/* + * Approach: + +Problem: +The problem involves implementing the Quick Sort algorithm to sort an array of integers in ascending order. + +Preface: +The program initializes an array of integers and sorts it using the quickSort() method. The quickSort() method recursively partitions the array and sorts its partitions using the split() method, which selects a pivot element and rearranges the array elements such that elements smaller than the pivot are on the left, and elements greater than the pivot are on the right. + +Logic: + +Define a method quickSort() to sort the array using the Quick Sort algorithm. +The quickSort() method partitions the array recursively using the split() method. +The split() method selects a pivot element, rearranges the array such that elements smaller than the pivot are on the left, and elements greater than the pivot are on the right. +The split() method returns the index of the pivot element. +Recursively call quickSort() on the left and right partitions of the array until the entire array is sorted. +Pseudocode: + +Initialize an array of integers. +Call quickSort() method with the array, starting index (0), and ending index (length - 1). +Define quickSort() method: +a. If the starting index is less than the ending index: +- Partition the array using the split() method. +- Recursively call quickSort() on the left and right partitions. +Define split() method: +a. Select a pivot element. +b. Iterate through the array and rearrange elements such that elements smaller than the pivot are on the left and elements greater than the pivot are on the right. +c. Return the index of the pivot element. +Define swap() method to swap two elements in the array. + +Time Complexity: +The time complexity of Quick Sort is O(n log n) on average and O(n^2) in the worst case, where n is the number of elements in the array. + +Output: +The program will output the sorted array in ascending order. + */ diff --git a/RemoveDuplicateElements.class b/RemoveDuplicateElements.class new file mode 100644 index 0000000000000000000000000000000000000000..45f46f007a224e844cd3ea91ecf8e91fde9af85a GIT binary patch literal 1711 zcmaJ>-&Y$&6#j;6GGV)vG&HmnQqiV0K$`fIDnXQ{MWP8?8w>{gHAzO;vf0hq-Jtl~ zKSE!9>8twCmfF+fv*-9X_~ifK@u7HklN>;z$vJap?%bL0es{mQzde8Q48St#Duxi^ zP&9-QVTj!mdm?8FYdfdc@5zS85Lq-V!&_nqrDv)tq8R2-HJpLQaN(|O*?aOVm9aTsSY$`R9W4)GI+m3`NfEdsJTgqtV@xfpitj$>F} z#dD-+rdY$;W*GMoyVnGiy8e8es9(}Bi78^x z61%csTa^8V;qB1O^+dqnTKe?K0hg(`j2AejHN1$I7{&%9&A{6}1=Au{gKn+H!ElM= zD#N+pd_ZVbxH}c;sYoHDB8?f2Sq*c@^rHe!Maz@h(jkmJVRoecl~4Nw>t00u46?{^ zyrSV%yv8tcghVJ*bA&#!n~h!!F{ch5~L8tQEM=_wPQY zQ4&QOD;nPS(~DA}t9~WuNWpHB87&!>T)1bm}XIY%rJShOHbfrz-R;;Dn3DtW7F5}7DFniU{DCd+Ov0Mt}F1aoL5AHzQ#Vo zcgH2y3+(2!Dy*g{-Bii8cRTI;>0%u7oH)3?-1*(w{9e{EGBL+@23-lJ`R_5!N3Z{*n2SpoW5S%tEEPMk$S8ih6gNY?gZW4aDi? zk71KKavcds>c}OEOhGwB3}KEkN0vrL<@^iD=ZO0YV)r+$5E(r{xPy<#o+Yd*5xpCX Q>-dy>Dy=`m7Z8~E2aNfnzyJUM literal 0 HcmV?d00001 diff --git a/RemoveDuplicateElements.java b/RemoveDuplicateElements.java new file mode 100644 index 0000000..1f6ea7a --- /dev/null +++ b/RemoveDuplicateElements.java @@ -0,0 +1,60 @@ +import java.util.*; + +public class RemoveDuplicateElements { + public static void main(String[] args) { + int[] arr = {1, 2, 3, 2, 4, 5, 3, 6}; + int[] result = removeDuplicates(arr); + System.out.println("Array with duplicates: " + Arrays.toString(arr)); + System.out.println("Array without duplicates: " + Arrays.toString(result)); + } + + public static int[] removeDuplicates(int[] arr) { + Set set = new HashSet<>(); + for (int num : arr) { + set.add(num); + } + + int[] result = new int[set.size()]; + int index = 0; + for (int num : set) { + result[index++] = num; + } + + return result; + } +} + +/* + * Approach: + +Problem: +The problem involves removing duplicate elements from an array. + +Preface: +The program takes an integer array as input and removes any duplicate elements from it using the removeDuplicates() method. + +Logic: + +Define a method removeDuplicates() to remove duplicate elements from the given integer array. +Use a HashSet to store unique elements of the array. +Iterate through the array and add each element to the HashSet. +Create a new array with the size of the HashSet to store unique elements. +Iterate through the HashSet and populate the new array with unique elements. +Return the new array containing unique elements. + +Pseudocode: + +Define an integer array to store the original array. +Call removeDuplicates() method with the original array. +Define removeDuplicates() method: +a. Initialize a HashSet to store unique elements. +b. Iterate through the original array and add each element to the HashSet. +c. Create a new integer array with the size of the HashSet. +d. Iterate through the HashSet and populate the new array with unique elements. +e. Return the new array. +Time Complexity: +The time complexity of removing duplicate elements is O(n), where n is the size of the original array. + +Output: +The program will output the original array with duplicates and the array without duplicates. + */ \ No newline at end of file