Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sandipkumardey authored Apr 6, 2024
1 parent 665d38c commit 0bb9173
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
Binary file added QuickSort.class
Binary file not shown.
73 changes: 73 additions & 0 deletions QuickSort.java
Original file line number Diff line number Diff line change
@@ -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.
*/
Binary file added RemoveDuplicateElements.class
Binary file not shown.
60 changes: 60 additions & 0 deletions RemoveDuplicateElements.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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.
*/

0 comments on commit 0bb9173

Please sign in to comment.