Skip to content

Commit 58c7a5c

Browse files
author
Shawn Bragdon
committed
Update documentation on the functions.
1 parent 1244068 commit 58c7a5c

File tree

17 files changed

+264
-163
lines changed

17 files changed

+264
-163
lines changed

.vscode/settings.json

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"files.associations": {
3+
"algorithm": "cpp",
4+
"array": "cpp",
5+
"atomic": "cpp",
6+
"bit": "cpp",
7+
"cctype": "cpp",
8+
"clocale": "cpp",
9+
"cmath": "cpp",
10+
"compare": "cpp",
11+
"concepts": "cpp",
12+
"cstddef": "cpp",
13+
"cstdint": "cpp",
14+
"cstdio": "cpp",
15+
"cstdlib": "cpp",
16+
"cstring": "cpp",
17+
"ctime": "cpp",
18+
"cwchar": "cpp",
19+
"exception": "cpp",
20+
"functional": "cpp",
21+
"initializer_list": "cpp",
22+
"iomanip": "cpp",
23+
"ios": "cpp",
24+
"iosfwd": "cpp",
25+
"iostream": "cpp",
26+
"istream": "cpp",
27+
"iterator": "cpp",
28+
"limits": "cpp",
29+
"list": "cpp",
30+
"memory": "cpp",
31+
"new": "cpp",
32+
"ostream": "cpp",
33+
"sstream": "cpp",
34+
"stdexcept": "cpp",
35+
"streambuf": "cpp",
36+
"string": "cpp",
37+
"system_error": "cpp",
38+
"tuple": "cpp",
39+
"type_traits": "cpp",
40+
"typeinfo": "cpp",
41+
"unordered_map": "cpp",
42+
"unordered_set": "cpp",
43+
"utility": "cpp",
44+
"vector": "cpp",
45+
"xfacet": "cpp",
46+
"xhash": "cpp",
47+
"xiosbase": "cpp",
48+
"xlocale": "cpp",
49+
"xlocinfo": "cpp",
50+
"xlocmon": "cpp",
51+
"xlocnum": "cpp",
52+
"xloctime": "cpp",
53+
"xmemory": "cpp",
54+
"xstddef": "cpp",
55+
"xstring": "cpp",
56+
"xtr1common": "cpp",
57+
"xutility": "cpp"
58+
}
59+
}

sortingAlgo/bubbleSort/bubbleSort.lua

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the bubble sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function bubbleSort(t)
25
local n = #t
36
for i = 1, n do
@@ -9,6 +12,10 @@ local function bubbleSort(t)
912
end
1013
end
1114

15+
--[[
1216
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
1317
bubbleSort(t)
14-
print(table.concat(t, ", "))
18+
print(table.concat(t, ", "))
19+
]]
20+
21+
return bubbleSort

sortingAlgo/bucketsort/bucketSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the bucket sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function bucketSort(t)
25
local n = #t
36
local buckets = {}
@@ -20,8 +23,7 @@ local function bucketSort(t)
2023
end
2124
end
2225
end
26+
return t
2327
end
2428

25-
local t = { 0.3, 0.2, 0.1, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 }
26-
bucketSort(t)
27-
print(table.concat(t, ", "))
29+
return bucketSort

sortingAlgo/cocktailSort/cocktailSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the cocktail sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function cocktailSort(t)
25
local n = #t
36
local swapped = true
@@ -24,8 +27,7 @@ local function cocktailSort(t)
2427
end
2528
start = start + 1
2629
end
30+
return t
2731
end
2832

29-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
30-
cocktailSort(t)
31-
print(table.concat(t, ", "))
33+
return cocktailSort

sortingAlgo/combSort/combSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using comb sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function combSort(t)
25
local n = #t
36
local gap = n
@@ -16,8 +19,7 @@ local function combSort(t)
1619
end
1720
end
1821
end
22+
return t
1923
end
2024

21-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
22-
combSort(t)
23-
print(table.concat(t, ", "))
25+
return combSort

sortingAlgo/countingSort/countingSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using counting sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function countingSort(t)
25
local n = #t
36
local counts = {}
@@ -13,8 +16,7 @@ local function countingSort(t)
1316
end
1417
end
1518
end
19+
return t
1620
end
1721

18-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
19-
countingSort(t)
20-
print(table.concat(t, ", "))
22+
return countingSort

sortingAlgo/cycleSort/cycleSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using cycle sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function cycleSort(t)
25
local n = #t
36
for i = 1, n do
@@ -29,8 +32,7 @@ local function cycleSort(t)
2932
t[pos], item = item, t[pos]
3033
end
3134
end
35+
return t
3236
end
3337

34-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
35-
cycleSort(t)
36-
print(table.concat(t, ", "))
38+
return cycleSort

sortingAlgo/insertionSort/insertionSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the insertion sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function insertionSort(t)
25
local n = #t
36
for i = 2, n do
@@ -9,8 +12,7 @@ local function insertionSort(t)
912
end
1013
t[j + 1] = item
1114
end
15+
return t
1216
end
1317

14-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
15-
insertionSort(t)
16-
print(table.concat(t, ", "))
18+
return insertionSort

sortingAlgo/mergeSort/mergeSort.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the merge sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function mergeSort(t)
25
local n = #t
36
if n < 2 then
@@ -40,6 +43,4 @@ local function mergeSort(t)
4043
return t
4144
end
4245

43-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
44-
mergeSort(t)
45-
print(table.concat(t, ", "))
46+
return mergeSort

sortingAlgo/pigeonholeSort/pigeonholeSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the pigeonhole sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function pigeonholeSort(t)
25
local n = #t
36
local min = math.huge
@@ -25,8 +28,7 @@ local function pigeonholeSort(t)
2528
holes[i] = holes[i] - 1
2629
end
2730
end
31+
return t
2832
end
2933

30-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
31-
pigeonholeSort(t)
32-
print(table.concat(t, ", "))
34+
return pigeonholeSort

sortingAlgo/quickSort/quickSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the quick sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function quickSort(t)
25
local n = #t
36
local function sort(t, start, end_)
@@ -17,8 +20,7 @@ local function quickSort(t)
1720
end
1821
end
1922
sort(t, 1, n)
23+
return t
2024
end
2125

22-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
23-
quickSort(t)
24-
print(table.concat(t, ", "))
26+
return quickSort

sortingAlgo/radixSort/radixSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the radix sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function radixSort(t)
25
local n = #t
36
local max = 0
@@ -27,8 +30,7 @@ local function radixSort(t)
2730
end
2831
exp = exp * 10
2932
end
33+
return t
3034
end
3135

32-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
33-
radixSort(t)
34-
print(table.concat(t, ", "))
36+
return radixSort

sortingAlgo/selectionSort/selectionSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the selection sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function selectionSort(t)
25
local n = #t
36
for i = 1, n - 1 do
@@ -9,8 +12,7 @@ local function selectionSort(t)
912
end
1013
t[i], t[min] = t[min], t[i]
1114
end
15+
return t
1216
end
1317

14-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
15-
selectionSort(t)
16-
print(table.concat(t, ", "))
18+
return selectionSort

sortingAlgo/shellSort/Readme.md

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
## Shell Sort
2+
23
Shell sort is an algorithm that first sorts the elements far apart from each other and successively reduces the interval between the elements to be sorted. It is a generalized version of insertion sort.
34

45
In shell sort, elements at a specific interval are sorted. The interval between the elements is gradually decreased based on the sequence used. The performance of the shell sort depends on the type of sequence used for a given input array.
56

67
shellSort(array, size)
7-
for interval i <- size/2n down to 1
8-
for each interval "i" in array
9-
sort all the elements at interval "i"
10-
end shellSort
8+
for interval i <- size/2n down to 1
9+
for each interval "i" in array
10+
sort all the elements at interval "i"
11+
end shellSort
12+
1113
## Complexity
14+
1215
Shell sort is an unstable sorting algorithm because this algorithm does not examine the elements lying in between the intervals.
16+
1317
### Time Complexities:
14-
* #### Worst Case Complexity: less than or equal to O(n<sup>2</sup>)
15-
* #### Best Case Complexities: O(n*log n)
16-
When the array is already sorted, the total number of comparisons for each interval (or increment) is equal to the size of the array.
17-
* #### Average Case Complexity: O(n*log n)
18-
It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
18+
19+
- #### Worst Case Complexity: less than or equal to O(n<sup>2</sup>)
20+
- #### Best Case Complexities: O(n\*log n)
21+
When the array is already sorted, the total number of comparisons for each interval (or increment) is equal to the size of the array.
22+
- #### Average Case Complexity: O(n\*log n)
23+
It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
1924

2025
### Space Complexity:
26+
2127
The space complexity for shell sort is **O(1)**
28+
2229
### Shell Sort Applications:
30+
2331
Shell sort is used when
32+
2433
1. calling a stack is overhead.
2534
2. recursion exceeds a limit.
2635
3. Insertion sort does not perform well when the close elements are far apart. Shell sort helps in reducing the distance between the close elements. Thus, there will be less number of swappings to be performed.
2736

28-
### Instruction for Running code:
37+
### Instruction for Running code:
2938

3039
- Python
31-
```
32-
python3 ShellSort.py
33-
```
40+
```
41+
python3 ShellSort.py
42+
```

sortingAlgo/shellSort/shellSort.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
--- Sort an array using the shell sort algorithm.
2+
--- @param t table
3+
--- @return table
14
local function shellSort(t)
25
local n = #t
36
local gap = math.floor(n / 2)
@@ -13,8 +16,7 @@ local function shellSort(t)
1316
end
1417
gap = math.floor(gap / 2)
1518
end
19+
return t
1620
end
1721

18-
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
19-
shellSort(t)
20-
print(table.concat(t, ", "))
22+
return shellSort

0 commit comments

Comments
 (0)