Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python120 #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions Java/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import java.util.Scanner;
// Java program for implementation of Insertion Sort
class InsertionSort {
/*Function to sort array using insertion sort*/
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;

public class InsertionSort {
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " elements :");
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
/* A utility function to print array of size n*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");

insertionSort(arr);
System.out.println();
}

System.out.println("\nThe sorted array : ;");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };

static void insertionSort(int arr[]) {
int len = arr.length, tmp, j;
for (int i = 1; i < len; i++) {
tmp = arr[i];
for (j = i; j > 0; j--) {
if (arr[j - 1] > tmp)
arr[j] = arr[j - 1];
else
break;
}
arr[j] = tmp;
}
}
InsertionSort ob = new InsertionSort();
ob.sort(arr);

printArray(arr);
}
}
42 changes: 22 additions & 20 deletions Python/SelectionSort.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# Python program for implementation of Selection Sort
from typing import List, TypeVar
# Python program for implementation of Selection
# Sort
import sys

# Declare generic type for the sort function input
T = TypeVar("T")

# Traverse through all array elements
for i in range(len(A)):

# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j

# Swap the found minimum element with
# the first element
A[i], A[min_idx] = A[min_idx], A[i]

# Driver code to test above

# Function to do selection sort
def selection_sort(arr: List[T]) -> None:
# Traverse through all array elements
for i in range(len(arr)):
# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i + 1, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j

# Swap the found minimum element with
# the first element
arr[i], arr[min_idx] = arr[min_idx], arr[i]


# Driver code to test above
if __name__ == "__main__":
a = [64, 25, 12, 22, 11]
selection_sort(a)
print("Sorted array", a)
A = [64, 25, 12, 22, 11]
print ("Sorted array")
for i in range(len(A)):
print("%d" %A[i]),