Skip to content

Commit e2b1b9e

Browse files
committed
new staff and cleaning
1 parent e90fb03 commit e2b1b9e

File tree

95 files changed

+1785
-231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1785
-231
lines changed

Diff for: Sorting/BogoSort/Python/BogoSort.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import random
2+
import unittest
3+
4+
# returns the sorted version of the given array by
5+
# checking first if its sorted, and then if not
6+
# randomly shuffles elements in the given array
7+
# and recursivly calls itself with the newly
8+
# shuffled array as an arguement.
9+
def monkeySort(arr1):
10+
for i in range(1, len(arr1)):
11+
if arr1[i-1] > arr1[i]:
12+
random.shuffle(arr1)
13+
return monkeySort(arr1)
14+
return arr1
15+
16+
class testMonkeySort(unittest.TestCase):
17+
18+
def test_regular_arr(self):
19+
arrPractice = [3, 4, 2, 1 ,5]
20+
self.assertEqual(monkeySort(arrPractice), [1, 2, 3, 4, 5])
21+
22+
def test_same_arr(self):
23+
arrPractice = [1, 1, 1, 1, 1]
24+
self.assertEqual(monkeySort(arrPractice), arrPractice)
25+
26+
def test_empty_arr(self):
27+
arrPractice = []
28+
self.assertEqual(monkeySort(arrPractice), arrPractice)
29+
30+
def test_duplicate_arr(self):
31+
arrPractice = [4, 2, 2, 1, 1]
32+
arrSolution = [1, 1, 2, 2, 4]
33+
self.assertEqual(monkeySort(arrPractice), arrSolution)
34+
35+
if __name__ == "__main__":
36+
unittest.main()

Diff for: Sorting/BogoSort/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>BogoSort</h1>
2+
<p>In computer science, bogosort (also permutation sort, stupid sort, slowsort, shotgun sort or monkey sort) is a highly ineffective sorting function based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms.</p>
3+
4+
<a href="https://en.wikipedia.org/wiki/Bogosort">Source: Wikipedia</a>

Diff for: Sorting/Bucket Sort/C++/Bucket_Sort.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
8+
void bucketSort(float a[], int n)
9+
{
10+
vector<float> t[n];
11+
12+
for (int i=0; i<n; i++)
13+
{
14+
int x = n*a[i];
15+
t[x].push_back(a[i]);
16+
}
17+
18+
for (int i=0; i<n; i++)
19+
sort(t[i].begin(), t[i].end());
20+
21+
int k= 0;
22+
for (int i = 0; i < n; i++)
23+
for (int j = 0; j < t[i].size(); j++)
24+
a[k++] = t[i][j];
25+
}
26+
27+
int main()
28+
{
29+
float a[] = {0.765, 0.324, 0.111, 0.951, 0.245, 0.48};
30+
int n = sizeof(a)/sizeof(a[0]);
31+
cout << "Elements before Sorting\n";
32+
for (int i=0; i<n; i++)
33+
cout << a[i] << " ";
34+
35+
bucketSort(a, n);
36+
37+
cout << "\nElements After Sorting \n";
38+
for (int i=0; i<n; i++)
39+
cout << a[i] << " ";
40+
return 0;
41+
}

Diff for: Sorting/Bucket Sort/Java/Bucket_Sort.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
//O(nlogn)
3+
public class BucketSort{
4+
5+
public static void bucketSort(int[] a) {
6+
int maxVal=a[0];
7+
for(int i=0;i<a.length;i++){
8+
if(a[i]>maxVal){
9+
maxVal=a[i];
10+
}
11+
}
12+
int [] bucket=new int[maxVal+1];
13+
14+
System.out.println("\n");
15+
for (int i=0; i<a.length; i++) {
16+
bucket[a[i]]++;
17+
//System.out.print(bucket[a[i]-1]);
18+
}
19+
20+
int outPos=0;
21+
for (int i=0; i<bucket.length; i++) {
22+
if(bucket[i]!=0){
23+
for (int j=0; j<bucket[i]; j++) {
24+
a[outPos++]=i;
25+
System.out.println(bucket[i]);
26+
}
27+
}
28+
}
29+
}
30+
31+
32+
33+
public static void main(String[] args) {
34+
int [] data= {10,23,53,22,1,1,100,32,58,34,42,64};
35+
36+
System.out.println("Before: " + Arrays.toString(data));
37+
bucketSort(data);
38+
System.out.println("After: " + Arrays.toString(data));
39+
}
40+
}

Diff for: Sorting/Bucket Sort/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>Bucket Sort</h1>
2+
<p>Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, a generalization of pigeonhole sort, and is a cousin of radix sort in the most-to-least significant digit flavor. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational complexity estimates involve the number of buckets.</p>
3+
<img src="https://upload.wikimedia.org/wikipedia/commons/5/54/Sorting_bubblesort_anim.gif">
4+
5+
<a href="https://en.wikipedia.org/wiki/Bucket_sort">Source: Wikipedia</a>

Diff for: Sorting/Cocktail Shaker Sort/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>Cocktail Shaker Sort</h1>
2+
<p>Cocktail shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts. It provides only marginal performance improvements, and does not improve asymptotic performance; like the bubble sort, it is not of practical interest (insertion sort is preferred for simple sorts), though it finds some use in education.</p>
3+
<img src="https://en.wikipedia.org/wiki/Cocktail_shaker_sort">
4+
5+
<a href="https://en.wikipedia.org/wiki/Bubble_sort">Source: Wikipedia</a>

Diff for: Sorting/CocktailShakerSort/README.md

-15
This file was deleted.

Diff for: Sorting/Comb Sort/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>Comb Sort</h1>
2+
<p>Comb sort is a relatively simple sorting algorithm originally designed by Włodzimierz Dobosiewicz in 1980. Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort.</p>
3+
<img src="https://upload.wikimedia.org/wikipedia/commons/4/46/Comb_sort_demo.gif">
4+
5+
<a href="https://en.wikipedia.org/wiki/Comb_sort">Source: Wikipedia</a>

Diff for: Sorting/Comb Sort/Ruby/Comb_Sort.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always
2+
# compares adjacent values. So all inversions are removed one by one. Comb Sort
3+
# improves on Bubble Sort by using gap of size more than 1. The gap starts with
4+
# a large value and shrinks by a factor of 1.3 in every iteration until it
5+
# reaches the value 1. Thus Comb Sort removes more than one inversion counts
6+
# with one swap and performs better than Bublle Sort.
7+
#
8+
# More on: https://www.geeksforgeeks.org/comb-sort/
9+
# Illustration gif: https://upload.wikimedia.org/wikipedia/commons/4/46/Comb_sort_demo.gif
10+
11+
def combsort(array, shrink = 1.247330950103979)
12+
sorted = array.dup
13+
gap = array.size
14+
swapped = false
15+
16+
until gap == 1 && !swapped
17+
gap = (gap / shrink).to_i
18+
gap = 1 if gap < 1
19+
20+
swapped = false
21+
22+
gap.upto(sorted.size - 1) do |i|
23+
next if sorted[i - gap] <= sorted[i]
24+
25+
sorted[i - gap], sorted[i] = sorted[i], sorted[i - gap]
26+
swapped = true
27+
end
28+
end
29+
30+
sorted
31+
end
32+
33+
# Test case
34+
if $PROGRAM_NAME == __FILE__
35+
unsorted = Array.new(20) { rand(10_000) }
36+
sorted = combsort(unsorted)
37+
puts "Sorting: #{unsorted}"
38+
puts "Sorted: #{sorted}"
39+
puts "Correct? #{sorted == unsorted.sort}"
40+
end
File renamed without changes.

Diff for: Sorting/Counting Sort/JavaScript/Couting_Sort.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countingSort(arr, min, max)
2+
{
3+
var i, z = 0, count = [];
4+
5+
for (i = min; i <= max; i++) {
6+
count[i] = 0;
7+
}
8+
9+
for (i=0; i < arr.length; i++) {
10+
count[arr[i]]++;
11+
}
12+
13+
for (i = min; i <= max; i++) {
14+
while (count[i]-- > 0) {
15+
arr[z++] = i;
16+
}
17+
}
18+
return arr;
19+
}

Diff for: Sorting/Counting Sort/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>Counting Sort</h1>
2+
<p>In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.</p>
3+
4+
<a href="https://en.wikipedia.org/wiki/Counting_sort">Source: Wikipedia</a>

Diff for: Sorting/HeapSort/C Sharp/HeapSort.cs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
3+
namespace Heap_sort
4+
{
5+
public class MainClass
6+
{
7+
public static void Main (string[] args)
8+
{
9+
int[] mykeys = new int[] {2, 5, -4, 11, 0, 18, 22, 67, 51, 6};
10+
11+
//double[] mykeys = new double[] {2.22, 0.5, 2.7, -1.0, 11.2};
12+
13+
//string[] mykeys = new string[] {"Red", "White", "Black", "Green", "Orange"};
14+
15+
Console.WriteLine("\nOriginal Array Elements :");
16+
printArray (mykeys);
17+
18+
heapSort (mykeys);
19+
20+
Console.WriteLine("\n\nSorted Array Elements :");
21+
printArray (mykeys);
22+
Console.WriteLine("\n");
23+
}
24+
25+
private static void heapSort<T> (T[] array) where T : IComparable<T>
26+
{
27+
int heapSize = array.Length;
28+
29+
buildMaxHeap (array);
30+
31+
for (int i = heapSize-1; i >= 1; i--)
32+
{
33+
swap (array, i, 0);
34+
heapSize--;
35+
sink (array, heapSize, 0);
36+
}
37+
}
38+
39+
private static void buildMaxHeap<T> (T[] array) where T : IComparable<T>
40+
{
41+
int heapSize = array.Length;
42+
43+
for (int i = (heapSize/2) - 1; i >= 0; i--)
44+
{
45+
sink (array, heapSize, i);
46+
}
47+
}
48+
49+
private static void sink<T> (T[] array, int heapSize, int toSinkPos) where T : IComparable<T>
50+
{
51+
if (getLeftKidPos (toSinkPos) >= heapSize)
52+
{
53+
// No left kid => no kid at all
54+
return;
55+
}
56+
57+
58+
int largestKidPos;
59+
bool leftIsLargest;
60+
61+
if (getRightKidPos (toSinkPos) >= heapSize || array [getRightKidPos (toSinkPos)].CompareTo (array [getLeftKidPos (toSinkPos)]) < 0)
62+
{
63+
largestKidPos = getLeftKidPos (toSinkPos);
64+
leftIsLargest = true;
65+
} else
66+
{
67+
largestKidPos = getRightKidPos (toSinkPos);
68+
leftIsLargest = false;
69+
}
70+
71+
72+
73+
if (array [largestKidPos].CompareTo (array [toSinkPos]) > 0)
74+
{
75+
swap (array, toSinkPos, largestKidPos);
76+
77+
if (leftIsLargest)
78+
{
79+
sink (array, heapSize, getLeftKidPos (toSinkPos));
80+
81+
} else
82+
{
83+
sink (array, heapSize, getRightKidPos (toSinkPos));
84+
}
85+
}
86+
87+
}
88+
89+
private static void swap<T> (T[] array, int pos0, int pos1)
90+
{
91+
T tmpVal = array [pos0];
92+
array [pos0] = array [pos1];
93+
array [pos1] = tmpVal;
94+
}
95+
96+
private static int getLeftKidPos (int parentPos)
97+
{
98+
return (2 * (parentPos + 1)) - 1;
99+
}
100+
101+
private static int getRightKidPos (int parentPos)
102+
{
103+
return 2 * (parentPos + 1);
104+
}
105+
106+
private static void printArray<T> (T[] array)
107+
{
108+
109+
foreach (T t in array)
110+
{
111+
Console.Write(' '+t.ToString()+' ');
112+
}
113+
114+
}
115+
}
116+
117+
}
File renamed without changes.

0 commit comments

Comments
 (0)