Skip to content

Commit

Permalink
Merge pull request #1 from diptangsu/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
iamsank7 committed Oct 6, 2021
2 parents 9a10118 + 07f32cd commit 64a17d5
Show file tree
Hide file tree
Showing 52 changed files with 1,859 additions and 304 deletions.
19 changes: 19 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version = 1

[[analyzers]]
name = "python"
enabled = true

[analyzers.meta]
runtime_version = "3.x.x"

[[analyzers]]
name = "go"
enabled = true

[analyzers.meta]
import_paths = ["github.com/diptangsu/Sorting-Algorithms"]

[[analyzers]]
name = "ruby"
enabled = true
86 changes: 50 additions & 36 deletions C/BubbleSort.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
#include <stdio.h>

void bubblesort(int a[], int size) {
for (int i = 0; i < size-1; i++) {
int swapped=0;
for (int j = 0; j < n-i-1; j++) {
if (a[j] > a[j+1]){
int tmp = a[j+1];
a[j+1] = a[j];
a[j] = tmp;
swapped=1;
#include <stdlib.h>
int swapped = 0; // global variable to check if swap() function is called
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
swapped++;
}
int bubblesort(int *a, int size) // to demonstrate passing by refference in C (pointer variable receives only the base address of array)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (a[j] > a[j + 1])
{
swap(a+j, a+j+1); // making a function is good instead of many lines of code in main function
}
}
if(swapped==0)
break;
if (swapped == 0)
return 1; // use return values better than break statement
}
return 0; // sorted

}

int main ()
{
int n, i;
int *a;
printf("Please insert the number of elements to be sorted: ");
scanf("%d", &n); // The total number of elements
a = (int *)calloc(n, sizeof(int));
for (i = 0;i< n;i++)
{
printf("Input element %d :", i);
scanf("%d", &a[i]); // Adding the elements to the array
}
printf("unsorted list: "); // Displaying the unsorted array
for(i = 0;i < n;i++)
{
printf("%d ", a[i]);
}
bubblesort(a, n);
printf("\nSorted list:\n"); // Display the sorted array
for(i = 0;i < n;i++)
{
printf("%d ", (a[i]));
}
return 0;
int main()
{
int n, i;
int *a = NULL; // security issue
printf("Please insert the number of elements to be sorted: ");
do
{
scanf("%d", &n); // The total number of elements
} while(n > 21); // never trust the user , heap-overflow , max elements 20 here

a = (int *)malloc(n * sizeof(int)); // Note: It would be better to use malloc over calloc, unless we want the zero-initialization because malloc is faster than calloc. So if we just want to copy some stuff or do something that doesn’t require filling of the blocks with zeros, then malloc would be a better choice.
for ( i = 0; i < n; i++)
{
printf("Input element %d :", i);
scanf("%d", (a+i)); // Adding the elements to the array
}
printf("unsorted list: "); // Displaying the unsorted array
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
bubblesort(a, n);
printf("\nSorted list:\n"); // Display the sorted array
for (i = 0; i < n; i++)
{
printf("%d ", *(a+i));// we're using C , so demonstrate pointers :)
}
return 0;
}
6 changes: 3 additions & 3 deletions C/cycleSort.c → C/CycleSort.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ int main(int argc, char ** argv)
int arr_k = sizeof(arr) / sizeof(arr[0]);
int writes, i;
printf("Original Array:\n");
show_array(arr, arr_k);
showArray(arr, arr_k);
writes = cycleSort(arr, arr_k);
printf("\nSorted Array:\n");
show_array(arr, arr_k);
showArray(arr, arr_k);
printf("writes: %d\n", writes);

return 0;
}

void show_array(int * array, size_t a_len)
void showArray(int * array, size_t a_len)
{
int ix;
for (ix = 0; ix < a_len; ++ix)
Expand Down
71 changes: 71 additions & 0 deletions C/PancakeSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>

void doFlip(int *, int, int);

int pancakeSort(int *list, unsigned int length)
{
if (length < 2)
return 0;
int i, a, max_num_pos, moves;

moves = 0;
for (i = length;i > 1;i--)
{
max_num_pos = 0;
for (a = 0;a < i;a++)
{
if (list[a] > list[max_num_pos])
max_num_pos = a;
}
if (max_num_pos == i - 1)
continue;
if (max_num_pos)
{
moves++;
doFlip(list, length, max_num_pos + 1);
}
doFlip(list, length, i);
}
return moves;
}


void doFlip(int *list, int length, int num)
{
int swap;
int i = 0;
for (i;i < --num;i++)
{
swap = list[i];
list[i] = list[num];
list[num] = swap;
}
}


void printArray(int list[], int length)
{
int i;
for (i = 0;i < length;i++)
{
printf("%d ", list[i]);
}
}

int main(int argc, char **argv)
{
int n;
scanf("%d",&n);
int list[n];
int i;
printf("enter the n elements of array:\n");
for (i = 0;i < n;i++)
scanf("%d", &list[i]);
printf("\nOriginal: ");
printArray(list, n);
int moves = pancakeSort(list, n);
printf("\nSorted: ");
printArray(list, n);
printf(" - with a total of %d moves\n", moves);
}
40 changes: 40 additions & 0 deletions Cpp/BucketSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


// C++ program to sort an array using the bucket sort method
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// to sort arr[] of size n using bucket sort
void bucketSort(float arr[], int n)
{

vector<float> b[n];
// Put array elements in different buckets
for (int i = 0; i < n; i++) {
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}
// sorting the buckets
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());
// add all the buckets into arr.
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}
//main driver code
int main()
{
float arr[] = { 0.878, 0.463, 0.616, 0.1234, 0.665, 0.3154 };
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);

cout << "here is the sorted array: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
};

36 changes: 36 additions & 0 deletions Cpp/CocktailSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
using namespace std;
void cocktailSort(int arr[], int n){
bool flag = true;
int start = 0, end = n-1;
while(flag){
flag = false;
for(int i = start; i<end; i++){ //scan from left to right as bubble sort
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
flag = true;
}
}
if(!flag){ //if nothing has changed simply break the loop
break;
}
flag = false;
end--; //decrease the end pointer
for(int i = end - 1; i >= start; i--){ //scan from right to left
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
flag = true;
}
}
start++;
}
}
main() {
int data[] = {54, 74, 98, 154, 98, 32, 20, 13, 35, 40};
int n = sizeof(data)/sizeof(data[0]);
cout << "Sorted Sequence ";
cocktailSort(data, n);
for(int i = 0; i <n;i++){
cout << data[i] << " ";
}
}
39 changes: 39 additions & 0 deletions Cpp/Cyclesort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>

void cycleSort(std::vector<int> &arr) {
for (int currentIndex = 0; currentIndex < arr.size() - 1; currentIndex++) {
int item = arr[currentIndex];
int currentIndexCopy = currentIndex;

for (int i = currentIndex + 1; i < arr.size(); i++)
if (arr[i] < item)
currentIndexCopy++;

if (currentIndexCopy == currentIndex)
continue;

while (item == arr[currentIndexCopy])
currentIndexCopy++;

int temp = arr[currentIndexCopy];
arr[currentIndexCopy] = item;
item = temp;

while (currentIndexCopy != currentIndex) {

currentIndexCopy = currentIndex;

for (int i = currentIndex + 1; i < arr.size(); i++)
if (arr[i] < item)
currentIndexCopy++;

while (item == arr[currentIndexCopy])
currentIndexCopy++;

temp = arr[currentIndexCopy];
arr[currentIndexCopy] = item;
item = temp;
}
}
}
75 changes: 75 additions & 0 deletions Cpp/Introsort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

void insertionsort(int array[], int low, int high)
{
for (int i = low + 1; i <= high; i++)
{
int value = a[i];
int j = i;
while (j > low && array[j - 1] > value)
{
array[j] = arraya[j - 1];
j--;
}
array[j] = value;
}
}

int partition(int array[], int low, int high)
{
int pivot = array[high];
int pIndex = low;
for (int i = low; i < high; i++)
{
if (array[i] <= pivot)
{
swap(array[i], array[pIndex]);
pIndex++;
}
}
swap (array[pIndex], array[high]);
return pIndex;
}

void heapsort(int *begin, int *end)
{
make_heap(begin, end);
sort_heap(begin, end);
}

int randPartition(int array[], int low, int high)
{
int pivotIndex = rand() % (high - low + 1) + low;
swap(array[pivotIndex], array[high]);
return partition(array, low, high);
}

void introsort(int array[], int *begin, int *end, int maxdepth)
{
if ((end - begin) < 16) {
insertionsort(array, begin - array, end - array);
}
else if (maxdepth == 0) {
heapsort(begin, end + 1);
}
else {
int pivot = randPartition(array, begin - array, end - array);
introsort(array, begin, array + pivot - 1, maxdepth - 1);
introsort(array, array + pivot + 1, end, maxdepth - 1);
}
}

int main()
{
int array[] = { 5, 7, -8, 9, 10, 4, -7, 0, -12, 1, 6, 2, 3, -4, -15, 12 };
int n = sizeof(array) / sizeof(array[0]);
int maxdepth = log(n) * 2;
introsort(array, array, array + n - 1, maxdepth);
for (int i = 0; i < n; i++) {
cout << array[i] << " ";
}
return 0;
}

0 comments on commit 64a17d5

Please sign in to comment.