From c66f86f5767d99497a52724977a46bb0853a4d76 Mon Sep 17 00:00:00 2001 From: Raj Mandal <99534700+Raj-Mandal-20@users.noreply.github.com> Date: Thu, 26 Oct 2023 23:34:42 +0530 Subject: [PATCH 1/2] Create selectionSort.py selection sort code --- selectionSort.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 selectionSort.py diff --git a/selectionSort.py b/selectionSort.py new file mode 100644 index 00000000..b50ffdbd --- /dev/null +++ b/selectionSort.py @@ -0,0 +1,17 @@ +def selectionSort(array, size): + + for ind in range(size): + min_index = ind + + for j in range(ind + 1, size): + # select the minimum element in every iteration + if array[j] < array[min_index]: + min_index = j + # swapping the elements to sort the array + (array[ind], array[min_index]) = (array[min_index], array[ind]) + +arr = [-2, 45, 0, 11, -9,88,-97,-202,747] +size = len(arr) +selectionSort(arr, size) +print('The array after sorting in Ascending Order by selection sort is:') +print(arr) From 3fe54ee75982f370096451809f00269da3133565 Mon Sep 17 00:00:00 2001 From: Raj Mandal <99534700+Raj-Mandal-20@users.noreply.github.com> Date: Thu, 26 Oct 2023 23:40:16 +0530 Subject: [PATCH 2/2] Create BubbleSort.java --- BubbleSort.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 BubbleSort.java diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 00000000..bb088397 --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,46 @@ +import java.io.*; + +class BubbleSort { + + // An optimized version of Bubble Sort + static void bubbleSort(int arr[], int n) + { + int i, j, temp; + boolean swapped; + for (i = 0; i < n - 1; i++) { + swapped = false; + for (j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + + // Swap arr[j] and arr[j+1] + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + + // If no two elements were + // swapped by inner loop, then break + if (swapped == false) + break; + } + } + + // Function to print an array + static void printArray(int arr[], int size) + { + int i; + for (i = 0; i < size; i++) + System.out.print(arr[i] + " "); + System.out.println(); + } + public static void main(String args[]) + { + int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; + int n = arr.length; + bubbleSort(arr, n); + System.out.println("Sorted array: "); + printArray(arr, n); + } +}