diff --git a/Arrays-Sorting/src/Bubble_Sort/Bubble_Sort.py b/Arrays-Sorting/src/Bubble_Sort/Bubble_Sort.py new file mode 100644 index 00000000..ddc6be0a --- /dev/null +++ b/Arrays-Sorting/src/Bubble_Sort/Bubble_Sort.py @@ -0,0 +1,20 @@ +#Bubble Sorting by Master-Fury +#Worst and Average Case Time Complexity: O(n*n) + +def Bubble_Sort(arr): + l=len(arr) + for i in range(l): + for j in range(0,l-i-1): + if(arr[j]>arr[j+1]): + arr[j],arr[j+1]=arr[j+1],arr[j] + return (arr) + + +#Driver Code +A=[1,34,21,345,765,44,67,32] #Your array +res=Bubble_Sort(A) +print(res) + + +#DESCRIPTION +#Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. diff --git a/Arrays-Sorting/src/Insertion_Sort/Insertion_Sort.py b/Arrays-Sorting/src/Insertion_Sort/Insertion_Sort.py new file mode 100644 index 00000000..c1454878 --- /dev/null +++ b/Arrays-Sorting/src/Insertion_Sort/Insertion_Sort.py @@ -0,0 +1,23 @@ +#Insertion Sort by Master-Fury +#Time Complexity: O(n*n) + +def Insertion_Sort(arr): + for i in range(1,len(arr)): + pos=arr[i] + j=i-1 + while j>=0 and posarr[i+1]: + arr[i],arr[i+1]=arr[i+1],arr[i] + Bubble_sort_rec(arr,l-1) #Recursion + return arr + +#Driver Code +arr=[34,76,45,342,54,6,788,23] #Your array +l=len(arr) +Bubble_sort_rec(arr,l) +print(arr) + + + + +#Recursive Bubble Sort has no performance/implementation advantages, +#but can be a good question to check one’s understanding of Bubble Sort and recursion. diff --git a/Arrays-Sorting/src/Selection_Sort/selection_sort.py b/Arrays-Sorting/src/Selection_Sort/selection_sort.py new file mode 100644 index 00000000..92a9061a --- /dev/null +++ b/Arrays-Sorting/src/Selection_Sort/selection_sort.py @@ -0,0 +1,22 @@ +#Selection Sort by Master-Fury +#Time Complexity:O(n^2) + +def Selection_Sort(arr): + for i in range(len(arr)): + min_index=i + for j in range(i+1, len(arr)): + if arr[min_index]>arr[j]: + min_index=j + arr[i],arr[min_index]=arr[min_index],arr[i] + return arr; + +#Driver Code +A=[1,33,56,21,3,78,54] #Your array +res=Selection_Sort(A) +for i in range(len(res)): + print(res[i]) + + +#Description +#The selection sort algorithm sorts an array by repeatedly finding the minimum element +#(considering ascending order) from unsorted part and putting it at the beginning.