From a99139331da2a15234ec95a1c8fb68a7a51e5c52 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Mon, 5 Oct 2020 01:13:13 +0530 Subject: [PATCH 1/9] Create selection.py --- Python/Algorithms/selection.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Python/Algorithms/selection.py diff --git a/Python/Algorithms/selection.py b/Python/Algorithms/selection.py new file mode 100644 index 00000000..db8970a1 --- /dev/null +++ b/Python/Algorithms/selection.py @@ -0,0 +1,21 @@ +def selectionSort(array): + n = len(array) + for i in range(n): + # Initially, assume the first element of the unsorted part as the minimum. + minimum = i + + for j in range(i+1, n): + if (array[j] < array[minimum]): + # Update position of minimum element if a smaller element is found. + minimum = j + + # Swap the minimum element with the first element of the unsorted part. + temp = array[i] + array[i] = array[minimum] + array[minimum] = temp + + return array + +# Driver code +array = [13, 4, 9, 5, 3, 16, 12] +print(selectionSort(array)) From d0367efba6df8069878d00794991c79000193d76 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Sat, 17 Oct 2020 11:30:32 +0530 Subject: [PATCH 2/9] Update selection.py --- Python/Algorithms/selection.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Python/Algorithms/selection.py b/Python/Algorithms/selection.py index db8970a1..0b932f24 100644 --- a/Python/Algorithms/selection.py +++ b/Python/Algorithms/selection.py @@ -1,3 +1,23 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[ ]: + + +''' +Copyright [2020] [Priyanka prasad] +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +''' +# this is a code for selection sort +# Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end def selectionSort(array): n = len(array) for i in range(n): From e72454a6870fd8871984e0ad4ef5538f8082318f Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Sat, 17 Oct 2020 12:39:42 +0530 Subject: [PATCH 3/9] Update selection.py --- Python/Algorithms/selection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/Algorithms/selection.py b/Python/Algorithms/selection.py index 0b932f24..fcb88516 100644 --- a/Python/Algorithms/selection.py +++ b/Python/Algorithms/selection.py @@ -5,7 +5,7 @@ ''' -Copyright [2020] [Priyanka prasad] +Copyright [2020] [Priyanka prasad ] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at From b1106e50e6ca89bf8cbaee79368490229a0d3316 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Sat, 17 Oct 2020 16:16:34 +0530 Subject: [PATCH 4/9] Create MergeSort.py --- Python/Algorithms/MergeSort.py | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Python/Algorithms/MergeSort.py diff --git a/Python/Algorithms/MergeSort.py b/Python/Algorithms/MergeSort.py new file mode 100644 index 00000000..ad0575cd --- /dev/null +++ b/Python/Algorithms/MergeSort.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[ ]: + + +''' +Copyright [2020] [Priyanka prasad ] +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +''' +# Python program for implementation of MergeSort + +# Merges two subarrays of arr[]. +# First subarray is arr[l..m] +# Second subarray is arr[m+1..r] +def merge(arr, l, m, r): + n1 = m - l + 1 + n2 = r- m + + # create temp arrays + L = [0] * (n1) + R = [0] * (n2) + + # Copy data to temp arrays L[] and R[] + for i in range(0 , n1): + L[i] = arr[l + i] + + for j in range(0 , n2): + R[j] = arr[m + 1 + j] + + # Merge the temp arrays back into arr[l..r] + i = 0 # Initial index of first subarray + j = 0 # Initial index of second subarray + k = l # Initial index of merged subarray + + while i < n1 and j < n2 : + if L[i] <= R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + # Copy the remaining elements of L[], if there + # are any + while i < n1: + arr[k] = L[i] + i += 1 + k += 1 + + # Copy the remaining elements of R[], if there + # are any + while j < n2: + arr[k] = R[j] + j += 1 + k += 1 + +# l is for left index and r is right index of the +# sub-array of arr to be sorted +def mergeSort(arr,l,r): + if l < r: + + # Same as (l+r)//2, but avoids overflow for + # large l and h + m = (l+(r-1))//2 + + # Sort first and second halves + mergeSort(arr, l, m) + mergeSort(arr, m+1, r) + merge(arr, l, m, r) + + +# Driver code to test above +arr = [12, 11, 13, 5, 6, 7] +n = len(arr) +print ("Given array is") +for i in range(n): + print ("%d" %arr[i]), + +mergeSort(arr,0,n-1) +print ("\n\nSorted array is") +for i in range(n): + print ("%d" %arr[i]), From 7f69113485953caba76012cc0f6ba8afb548c256 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Sat, 17 Oct 2020 16:32:57 +0530 Subject: [PATCH 5/9] Create BubbleSort.py --- Python/Algorithms/BubbleSort.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Python/Algorithms/BubbleSort.py diff --git a/Python/Algorithms/BubbleSort.py b/Python/Algorithms/BubbleSort.py new file mode 100644 index 00000000..41d0aca3 --- /dev/null +++ b/Python/Algorithms/BubbleSort.py @@ -0,0 +1,38 @@ + +''' +Copyright [2020] [Priyanka prasad ] +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +''' +#--------------------------------------- +# Bubble Sort +#--------------------------------------- +# not optimized +def bubble_sort1(A): + for i in range (0, len(A) - 1): + for j in range (0, len(A) - i - 1): + if A[j] > A[j+1]: + A[j], A[j+1] = A[j+1], A[j] + +# optimized to exit if no swaps occur +def bubble_sort2(A): + for i in range (0, len(A) - 1): + done = True + for j in range (0, len(A) - i - 1): + if A[j] > A[j+1]: + A[j], A[j+1] = A[j+1], A[j] + done = False + if done: + return + +A = [5,9,1,2,4,8,6,3,7] +print(A) +bubble_sort1(A) +print(A) From dba607743e7ea7a87788097ee3f9623319c81295 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Sat, 17 Oct 2020 17:32:29 +0530 Subject: [PATCH 6/9] Delete selection.py --- Python/Algorithms/selection.py | 41 ---------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 Python/Algorithms/selection.py diff --git a/Python/Algorithms/selection.py b/Python/Algorithms/selection.py deleted file mode 100644 index fcb88516..00000000 --- a/Python/Algorithms/selection.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python -# coding: utf-8 - -# In[ ]: - - -''' -Copyright [2020] [Priyanka prasad ] -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -''' -# this is a code for selection sort -# Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end -def selectionSort(array): - n = len(array) - for i in range(n): - # Initially, assume the first element of the unsorted part as the minimum. - minimum = i - - for j in range(i+1, n): - if (array[j] < array[minimum]): - # Update position of minimum element if a smaller element is found. - minimum = j - - # Swap the minimum element with the first element of the unsorted part. - temp = array[i] - array[i] = array[minimum] - array[minimum] = temp - - return array - -# Driver code -array = [13, 4, 9, 5, 3, 16, 12] -print(selectionSort(array)) From fb78d8e46a2b5749fadadd3006451f5a9b065c1b Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Sat, 17 Oct 2020 17:35:21 +0530 Subject: [PATCH 7/9] Delete MergeSort.py --- Python/Algorithms/MergeSort.py | 92 ---------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 Python/Algorithms/MergeSort.py diff --git a/Python/Algorithms/MergeSort.py b/Python/Algorithms/MergeSort.py deleted file mode 100644 index ad0575cd..00000000 --- a/Python/Algorithms/MergeSort.py +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/env python -# coding: utf-8 - -# In[ ]: - - -''' -Copyright [2020] [Priyanka prasad ] -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -''' -# Python program for implementation of MergeSort - -# Merges two subarrays of arr[]. -# First subarray is arr[l..m] -# Second subarray is arr[m+1..r] -def merge(arr, l, m, r): - n1 = m - l + 1 - n2 = r- m - - # create temp arrays - L = [0] * (n1) - R = [0] * (n2) - - # Copy data to temp arrays L[] and R[] - for i in range(0 , n1): - L[i] = arr[l + i] - - for j in range(0 , n2): - R[j] = arr[m + 1 + j] - - # Merge the temp arrays back into arr[l..r] - i = 0 # Initial index of first subarray - j = 0 # Initial index of second subarray - k = l # Initial index of merged subarray - - while i < n1 and j < n2 : - if L[i] <= R[j]: - arr[k] = L[i] - i += 1 - else: - arr[k] = R[j] - j += 1 - k += 1 - - # Copy the remaining elements of L[], if there - # are any - while i < n1: - arr[k] = L[i] - i += 1 - k += 1 - - # Copy the remaining elements of R[], if there - # are any - while j < n2: - arr[k] = R[j] - j += 1 - k += 1 - -# l is for left index and r is right index of the -# sub-array of arr to be sorted -def mergeSort(arr,l,r): - if l < r: - - # Same as (l+r)//2, but avoids overflow for - # large l and h - m = (l+(r-1))//2 - - # Sort first and second halves - mergeSort(arr, l, m) - mergeSort(arr, m+1, r) - merge(arr, l, m, r) - - -# Driver code to test above -arr = [12, 11, 13, 5, 6, 7] -n = len(arr) -print ("Given array is") -for i in range(n): - print ("%d" %arr[i]), - -mergeSort(arr,0,n-1) -print ("\n\nSorted array is") -for i in range(n): - print ("%d" %arr[i]), From 6629f75b23ac74fd6dd14b446ee7b41cf6f46d64 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Mon, 19 Oct 2020 05:43:08 +0530 Subject: [PATCH 8/9] Create SwapWithoutUsingtempvar.java --- Java/Swap/SwapWithoutUsingtempvar.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Java/Swap/SwapWithoutUsingtempvar.java diff --git a/Java/Swap/SwapWithoutUsingtempvar.java b/Java/Swap/SwapWithoutUsingtempvar.java new file mode 100644 index 00000000..a040c6f0 --- /dev/null +++ b/Java/Swap/SwapWithoutUsingtempvar.java @@ -0,0 +1,21 @@ + +// Java Program to swap two numbers without +// using temporary variable +import java.*; + +class Swap { + + public static void main(String a[]) + { + Scanner scan = new.Scanner(System.in); + System.out.print("Enter X Value: "); + int x = scan.nextInt(); + System.out.print("Enter Y Value: "); + int y = scan.nextInt(); + x = x + y; + y = x - y; + x = x - y; + System.out.println("After swaping:" + + " x = " + x + ", y = " + y); + } +} From 7bec594c055e2d3bd2106e3761bdf70f00cd5991 Mon Sep 17 00:00:00 2001 From: priyanka prasad <59612128+P-riyanka-prasad@users.noreply.github.com> Date: Mon, 19 Oct 2020 05:45:12 +0530 Subject: [PATCH 9/9] Update SwapWithoutUsingtempvar.java --- Java/Swap/SwapWithoutUsingtempvar.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Java/Swap/SwapWithoutUsingtempvar.java b/Java/Swap/SwapWithoutUsingtempvar.java index a040c6f0..427e297f 100644 --- a/Java/Swap/SwapWithoutUsingtempvar.java +++ b/Java/Swap/SwapWithoutUsingtempvar.java @@ -1,4 +1,15 @@ - +/* +Copyright [2020] [Priyanka prasad ] +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ // Java Program to swap two numbers without // using temporary variable import java.*;