Skip to content

Commit d09ff6b

Browse files
committed
i have added bunch of codes
1 parent d7389fc commit d09ff6b

File tree

10 files changed

+385
-0
lines changed

10 files changed

+385
-0
lines changed

Diff for: bubble_sort/C/bubble_sort.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdio.h>
2+
3+
void bubble_sort(int[], int);
4+
void print_array(int[], int);
5+
void swap(int*, int*);
6+
7+
int main() {
8+
int arr[] = {45, 92, 54, 23, 6, 4, 12};
9+
int n = sizeof(arr) / sizeof(arr[0]);
10+
bubble_sort(arr, n);
11+
printf("Sorted array: \n");
12+
print_array(arr, n);
13+
}
14+
15+
/* Function to swap two numbers */
16+
void swap(int *a, int *b) {
17+
int temp = *a;
18+
*a = *b;
19+
*b = temp;
20+
}
21+
22+
/* Bubble Sort algorithm */
23+
void bubble_sort(int arr[], int n) {
24+
int i, j;
25+
int swapped;
26+
for (i = 0; i < n - 1; ++i) {
27+
swapped = 0;
28+
for (j = 0; j < n - i - 1; ++j) {
29+
if (arr[j] > arr[j+1]) {
30+
swap(&arr[j], &arr[j+1]);
31+
swapped = 1;
32+
}
33+
}
34+
35+
/* If no two elements are swapped by inner loop, then break */
36+
if (swapped == 0)
37+
break;
38+
}
39+
}
40+
41+
/* Function to print array */
42+
void print_array(int arr[], int size) {
43+
int i;
44+
for (i = 0; i < size; i++)
45+
printf("%d ", arr[i]);
46+
printf("\n");
47+
}

Diff for: bubble_sort/Cpp/bubble_sort.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
3+
void swap(int *xp, int *yp) {
4+
int temp = *xp;
5+
*xp = *yp;
6+
*yp = temp;
7+
}
8+
9+
void bubble_sort(int arr[], int n) {
10+
int i, j;
11+
bool swapped;
12+
for (i = 0; i < n-1; i++) {
13+
swapped = false;
14+
for (j = 0; j < n-i-1; j++) {
15+
if (arr[j] > arr[j+1]) {
16+
swap(&arr[j], &arr[j+1]);
17+
swapped = true;
18+
}
19+
}
20+
21+
// IF no two elements were swapped by inner loop, then break
22+
if (swapped == false)
23+
break;
24+
}
25+
}
26+
27+
/* Function to print an array */
28+
void print_array(int arr[], int size) {
29+
int i;
30+
for (i = 0; i < size; i++)
31+
printf("%d ", arr[i]);
32+
printf("\n");
33+
}
34+
35+
// Driver program to test above functions
36+
int main() {
37+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
38+
int n = sizeof(arr) / sizeof(arr[0]);
39+
bubble_sort(arr, n);
40+
printf("Sorted array: \n");
41+
print_array(arr, n);
42+
return 0;
43+
}

Diff for: counting_sort/Python/counting_sort.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def counting_sort(arr, digit: int, radix=10):
2+
"""
3+
Fuction to sort using Counting Sort
4+
<https://en.wikipedia.org/wiki/Counting_sort>.
5+
6+
:param arr: A list of number to sort
7+
:param digit: The digit we want to sort by
8+
:param radix: The base of the number system
9+
:return result: The sorted list of given arr list
10+
"""
11+
12+
arr_len = len(arr)
13+
count = [0]*radix # counts the number of occurences of each digit in arr
14+
result = [0]*arr_len
15+
16+
def _digit_find(num, digit, radix):
17+
return int((num / radix ** digit) % radix)
18+
19+
for i in range(arr_len):
20+
digit_of_arr_i = _digit_find(arr[i], digit, radix)
21+
count[digit_of_arr_i] += 1
22+
23+
for j in range(1, radix):
24+
count[j] += count[j-1]
25+
26+
for m in range(arr_len-1, -1, -1):
27+
digit_of_arr_i = _digit_find(arr[m], digit, radix)
28+
count[digit_of_arr_i] -= 1
29+
result[count[digit_of_arr_i]] = arr[m]
30+
31+
return result
32+
33+
34+
def main():
35+
arr = [6, 5, 4, 3, 2, 1]
36+
print('Sorted element using Counting Sort: {}'.format(
37+
' '.join(map(str, counting_sort(arr, 0)))))
38+
39+
40+
if __name__ == '__main__':
41+
main()

Diff for: insertion_sort/C/insertion_sort.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
void insertion_sort(int arr_size, int *arr) {
5+
// Fuction to do insertion sort.
6+
int i;
7+
int pivot;
8+
int j;
9+
for (i = 1; i < arr_size; i++) {
10+
pivot = arr[i];
11+
j = i-1;
12+
/* Move elements of arr[0..i-1], that are
13+
greater than pivot, to one position ahead
14+
of their current position */
15+
while (j >= 0 && arr[j] > pivot) {
16+
arr[j+1] = arr[j];
17+
j = j-1;
18+
}
19+
arr[j+1] = pivot;
20+
}
21+
}
22+
23+
int main() {
24+
int arr_size = 6, i;
25+
int arr[6] = {6, 5, 4, 3, 2, 1};
26+
insertion_sort(arr_size, arr);
27+
28+
for (i=0; i < arr_size; i++) {
29+
printf("%d\n", arr[i]);
30+
}
31+
return 0;
32+
}

Diff for: insertion_sort/Cpp/insertion_sort.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void insertion_sort(int arr[], int n) {
6+
// Fuction to do insertion sort.
7+
for (int i = 1; i < 10; i++) {
8+
int temp;
9+
temp = arr[i];
10+
int x;
11+
for (int j = i; j >= 0; j--) {
12+
/* Move elements of arr[0..i-1], that are greater, to one position
13+
ahead of their current position */
14+
if (temp <= arr[j]) {
15+
arr[j] = arr[j-1];
16+
x = j;
17+
}
18+
}
19+
arr[x] = temp;
20+
}
21+
}
22+
23+
int main() {
24+
int arrsize = 6;
25+
int arr[arrsize] = {6, 1, 2, 4, 5, 3};
26+
insertion_sort(arr, arrsize);
27+
for (int i = 0; i < arrsize; i++) {
28+
cout << arr[i] << " ";
29+
}
30+
return 0;
31+
}

Diff for: insertion_sort/Python/insertion_sort.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def insertion_sort(arr):
2+
"""
3+
Fuction to do insertion sort.
4+
5+
:param arr: A list of element to sort.
6+
"""
7+
for slot in range(1, len(arr)):
8+
value = arr[slot]
9+
test_slot = slot - 1
10+
while test_slot > -1 and arr[test_slot] > value:
11+
arr[test_slot + 1] = arr[test_slot]
12+
test_slot = test_slot - 1
13+
arr[test_slot + 1] = value
14+
15+
return arr
16+
17+
18+
def main():
19+
arr = [6, 5, 4, 3, 2, 1]
20+
print('Sorted element using Insertion Sort: {}'.format(
21+
' '.join(map(str, insertion_sort(arr)))))
22+
23+
24+
if __name__ == '__main__':
25+
main()

Diff for: merge_sort/Python/merge_sort.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Merge Sort Algorthm
3+
- Create an empty list called the result list.
4+
- Do the following until one of the input lists is empty:
5+
- Remove the first element of the list that has a lesser first element
6+
and append it to the result list.
7+
- When one of the lists is empty, append all elements of the other list to
8+
the result.
9+
"""
10+
11+
12+
def _merge(left, right):
13+
result = []
14+
left_idx, right_idx = 0, 0
15+
while left_idx < len(left) and right_idx < len(right):
16+
# change the direction of this comparison to change
17+
# the direction of the sort
18+
if left[left_idx] <= right[right_idx]:
19+
result.append(left[left_idx])
20+
left_idx += 1
21+
else:
22+
result.append(right[right_idx])
23+
right_idx += 1
24+
25+
if left:
26+
result.extend(left[left_idx:])
27+
if right:
28+
result.extend(right[right_idx:])
29+
return result
30+
31+
32+
def merge_sort(arr):
33+
"""
34+
Fuction to do merge sort.
35+
36+
:param arr: A list of element to sort.
37+
"""
38+
if len(arr) <= 1:
39+
return arr
40+
41+
middle = len(arr) // 2
42+
left = arr[:middle]
43+
right = arr[middle:]
44+
45+
left = merge_sort(left)
46+
right = merge_sort(right)
47+
return list(_merge(left, right))
48+
49+
50+
def main():
51+
arr = [6, 5, 4, 3, 2, 1]
52+
print('Sorted element using Merge Sort: {}'.format(
53+
' '.join(map(str, merge_sort(arr)))))
54+
55+
56+
if __name__ == '__main__':
57+
main()

Diff for: quicksort/Python/quicksort.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
QuickSort Algorithm
3+
- If the list is empty, return the list and terminate. - (Base case)
4+
- Choose a pivot element in the list.
5+
- Take all of the elements that are less than or equal to the pivot and use
6+
quicksort on them.
7+
- Take all of the elements that are greater than the pivot and use quicksort
8+
on them.
9+
- Return the concatenation of the quicksorted list of elements that are less
10+
than or equal to the pivot, the pivot, and the quicksorted list of elements
11+
that are greater than the pivot.
12+
"""
13+
14+
15+
def quicksort(arr):
16+
"""
17+
Fuction to do quicksort.
18+
19+
:param arr: A list of element to sort.
20+
"""
21+
less = []
22+
pivotList = []
23+
more = []
24+
25+
if len(arr) <= 1:
26+
return arr
27+
else:
28+
pivot = arr[0]
29+
for i in arr:
30+
if i < pivot:
31+
less.append(i)
32+
elif i > pivot:
33+
more.append(i)
34+
else:
35+
pivotList.append(i)
36+
less = quicksort(less)
37+
more = quicksort(more)
38+
return less + pivotList + more
39+
40+
41+
def main():
42+
arr = [6, 5, 4, 3, 2, 1]
43+
print('Sorted element using Quicksort: {}'.format(
44+
' '.join(map(str, quicksort(arr)))))
45+
46+
47+
if __name__ == '__main__':
48+
main()

Diff for: radix_sort/Python/radix_sort.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def radix_sort(array, base=10):
2+
"""
3+
Fuction to sort using radix sort algorithm
4+
<https://en.wikipedia.org/wiki/Radix_sort>.
5+
6+
:param array: A list of elements to sort.
7+
:param base: Integer number to represent base number
8+
"""
9+
maxLen = len(str(max(array)))
10+
11+
for i in range(maxLen):
12+
digit_ref = [[] for _ in range(base)]
13+
for num in array:
14+
digit_ref[(num // base ** i) % base].append(num)
15+
array = []
16+
for section in digit_ref:
17+
array.extend(section)
18+
return array
19+
20+
21+
def main():
22+
array = [6, 5, 4, 3, 2, 1]
23+
print('Sorted element using Radix Sort: {}'.format(
24+
' '.join(map(str, radix_sort(array)))))
25+
26+
27+
if __name__ == '__main__':
28+
main()

Diff for: shell_sort/Python/shell_sort.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def shell_sort(arr):
2+
"""
3+
Fuction to sort using Shell Sort
4+
<https://en.wikipedia.org/wiki/Shellsort>.
5+
:param arr: A list of element to sort
6+
"""
7+
8+
gap = int((len(arr)/2))
9+
while gap > 0:
10+
for i in range(gap, len(arr)):
11+
temp = arr[i]
12+
j = i
13+
while j >= gap and arr[j - gap] > temp:
14+
arr[j] = arr[j-gap]
15+
j -= gap
16+
17+
arr[j] = temp
18+
19+
gap /= 2
20+
gap = int(gap)
21+
22+
return arr
23+
24+
25+
def main():
26+
arr = [15, 12, 36, 63, 96]
27+
sorted_arr = shell_sort(arr)
28+
print('Sorted element using Shell Sort: {}'.format(
29+
' '.join(map(str, shell_sort(arr)))))
30+
31+
32+
if __name__ == '__main__':
33+
main()

0 commit comments

Comments
 (0)