diff --git a/classical_algorithms/python/Bubble Sort.py b/classical_algorithms/python/Bubble Sort.py deleted file mode 100644 index b09433d1..00000000 --- a/classical_algorithms/python/Bubble Sort.py +++ /dev/null @@ -1,21 +0,0 @@ -def bubblesort(array): - - n = len(array) - - for i in range(0,n-1): - for j in range(0,n-i-1): - if (array[j] > array[j+1]): - array[j] , array[j+1] = array[j+1] , array[j] - - print(array) - - -m = int(input('Enter the number of elements in the array ')) - -mylist = [] - -for i in range(0,m): - num = int(input('Enter element: ')) - mylist.append(num) - -bubblesort(mylist) diff --git a/classical_algorithms/python/BubbleSort.py b/classical_algorithms/python/BubbleSort.py new file mode 100644 index 00000000..a9d40aec --- /dev/null +++ b/classical_algorithms/python/BubbleSort.py @@ -0,0 +1,22 @@ +def bubblesort(array): + n = len(array) + + for i in range(0, n - 1): + for j in range(0, n - i - 1): + if array[j] > array[j + 1]: + array[j], array[j + 1] = array[j + 1], array[j] + + return array + + +if __name__ == '__main__': + m = int(input('Enter the number of elements in the array ')) + + mylist = [] + + for i in range(0, m): + num = int(input('Enter element: ')) + mylist.append(num) + + output = bubblesort(mylist) + print(output) diff --git a/classical_algorithms/python/Insertion Sort.py b/classical_algorithms/python/Insertion Sort.py deleted file mode 100644 index 3cdc451d..00000000 --- a/classical_algorithms/python/Insertion Sort.py +++ /dev/null @@ -1,27 +0,0 @@ -def insertion_sort(array): - - for i in range(1,len(array)): - j = i-1 - key = array[i] - - #move all the elements of array[0,i-1] that are greater than - #the key, one position ahead of their current one - - while j>=0 and array[j]>key: - array[j+1] = array[j] - j = j-1 - - array[j+1] = key - - print(array) - - -m = int(input('Enter the number of elements in the array ')) - -mylist = [] - -for i in range(0,m): - num = int(input('Enter element: ')) - mylist.append(num) - -insertion_sort(mylist) \ No newline at end of file diff --git a/classical_algorithms/python/InsertionSort.py b/classical_algorithms/python/InsertionSort.py new file mode 100644 index 00000000..ec2292cc --- /dev/null +++ b/classical_algorithms/python/InsertionSort.py @@ -0,0 +1,21 @@ +def insertion_sort(array): + for i in range(1, len(array)): + j = i - 1 + key = array[i] + + while j >= 0 and array[j] > key: + array[j + 1] = array[j] + j = j - 1 + + array[j + 1] = key + + return array + + +if "__name__" == "__main__": + m = int(input('Enter the number of elements in the array ')) + mylist = [] + for i in range(0, m): + num = int(input('Enter element: ')) + mylist.append(num) + print(insertion_sort(mylist)) diff --git a/classical_algorithms/python/Sieve of Eratosthenes.py b/classical_algorithms/python/Sieve of Eratosthenes.py deleted file mode 100644 index 5cc7b341..00000000 --- a/classical_algorithms/python/Sieve of Eratosthenes.py +++ /dev/null @@ -1,28 +0,0 @@ -import math -# taking input -n = int(input()) -# making a list named primes - # initialize primes with 1 only -# here 1 represent the value is prime -primes = [1]*(n+1) - -# making first and second values as 0 -# as 0, 1 is not a prime and start by checking -# from second value -primes[0] = 0 -primes[1] = 0 - -# looping through 2,sqrt(n)+1 as every number -# comes with 2 factor if first factor is seen then -# we don't have to look for second one -# in second loop we are initialize all multiple of -# i in first loop -for i in range(2,int(math.sqrt(n)+1)): - if primes[i] ==1: - for j in range(i*i,n+1,i): - primes[j] = 0 - -# printing all primes -for i in range(len(primes)): - if primes[i] == 1: - print(i,end = " ") \ No newline at end of file diff --git a/classical_algorithms/python/sieveoferatosthenes.py b/classical_algorithms/python/sieveoferatosthenes.py new file mode 100644 index 00000000..1caf371d --- /dev/null +++ b/classical_algorithms/python/sieveoferatosthenes.py @@ -0,0 +1,23 @@ +import math + + +def sieve_of_eratosthenes(n): + if n < 2: + return [] + primes = [1] * (n + 1) + primes[0] = primes[1] = 0 + + for i in range(2, int(math.sqrt(n) + 1)): + if primes[i] == 1: + for j in range(i * i, n + 1, i): + primes[j] = 0 + + return [i for i in range(n + 1) if primes[i] == 1] + + +if "__name__" == "__main__": + n = int(input()) + primes = sieve_of_eratosthenes(n) + print(f"Prime numbers up to {n}:") + for prime in primes: + print(prime, end=" ") diff --git a/classical_algorithms/python/tests/test_bubble_sort.py b/classical_algorithms/python/tests/test_bubble_sort.py new file mode 100644 index 00000000..041a57f6 --- /dev/null +++ b/classical_algorithms/python/tests/test_bubble_sort.py @@ -0,0 +1,23 @@ +import unittest + +from classical_algorithms.python.BubbleSort import bubblesort + + +class TestBubbleSort(unittest.TestCase): + def test_bubble_sort(self): + # normal test 1 + self.assertEqual(bubblesort([64, 34, 25, 12, 22, 11, 90]), [11, 12, 22, 25, 34, 64, 90]) + # normal test 2 + self.assertEqual(bubblesort([5, 2, 9, 1, 5, 6]), [1, 2, 5, 5, 6, 9]) + # empty list + self.assertEqual(bubblesort([]), []) + # single-element list + self.assertEqual(bubblesort([1]), [1]) + # reverse-ordered list + self.assertEqual(bubblesort([9, 8, 7, 6, 5, 4, 3, 2, 1]), + [1, 2, 3, 4, 5, 6, 7, 8, 9]) + # negative list + self.assertEqual(bubblesort([-1, -2, -3, -4]), [-4, -3, -2, -1]) + +if __name__ == '__main__': + unittest.main() diff --git a/classical_algorithms/python/tests/test_insertionsort.py b/classical_algorithms/python/tests/test_insertionsort.py new file mode 100644 index 00000000..f207e324 --- /dev/null +++ b/classical_algorithms/python/tests/test_insertionsort.py @@ -0,0 +1,25 @@ +import unittest + +from classical_algorithms.python.InsertionSort import insertion_sort + + +class TestInsertionSort(unittest.TestCase): + + def test_insertion_sort(self): + # test case 1 + self.assertEqual(insertion_sort([64, 34, 25, 12, 22, 11, 90]), [11, 12, 22, 25, 34, 64, 90]) + # test case 2 + self.assertEqual(insertion_sort([5, 2, 9, 1, 5, 6]), [1, 2, 5, 5, 6, 9]) + # empty list + self.assertEqual(insertion_sort([]), []) + # single-element list + self.assertEqual(insertion_sort([1]), [1]) + # reverse-ordered list + self.assertEqual(insertion_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]), + [1, 2, 3, 4, 5, 6, 7, 8, 9]) + # negative list + self.assertEqual(insertion_sort([-1, -2, -3, -4]), [-4, -3, -2, -1]) + + +if __name__ == '__main__': + unittest.main() diff --git a/classical_algorithms/python/tests/test_sieveoferatosthenes.py b/classical_algorithms/python/tests/test_sieveoferatosthenes.py new file mode 100644 index 00000000..d2b0812b --- /dev/null +++ b/classical_algorithms/python/tests/test_sieveoferatosthenes.py @@ -0,0 +1,21 @@ +import unittest + +from classical_algorithms.python.sieveoferatosthenes import sieve_of_eratosthenes + + +class TestSieveOfEratosthenes(unittest.TestCase): + def test_sieve_of_eratosthenes(self): + #test case1 + self.assertEqual(sieve_of_eratosthenes(10), [2, 3, 5, 7]) + # test case2 + self.assertEqual(sieve_of_eratosthenes(20), [2, 3, 5, 7, 11, 13, 17, 19]) + # for 1 + self.assertEqual(sieve_of_eratosthenes(1), []) + # for 2 + self.assertEqual(sieve_of_eratosthenes(2), [2]) + # for negative number + self.assertEqual(sieve_of_eratosthenes(-1), []) + + +if __name__ == '__main__': + unittest.main()