Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting Algorithms #1724

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Bubble Sort == Reapetedly swap two adjacent elements if they are in wrong order
// For n elements in array == (n-1) iterations to get sorted array
// If ith iteration == Check upto (n-i)
#include<iostream>
using namespace std;

int main()
{
int n;
cin >> n;

int arr[n];


for(int i=0;i<n;i++)
{
cin >> arr[i];
}

int counter = 1;
while(counter < n-1)
{
for(int i=0;i<n-counter;i++)
{
if(arr[i]>arr[i+1])
{
// Swapping the numbers
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
counter++;
}

for(int i=0;i<n;i++)
{
cout << arr[i] << " ";
}





return 0;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Name: JHALA DEVRAJSINH SHRIPALSINH
// Date: 01/07/2021
// Purpose: Count Sort time complexity better than Quick Sort and Merge Sort
// Approach:
// 1. Find the count of every distinct element in the array
// 2. Calculate the position of each element in the sorted array

// Time Complexity:
// max(Ai,N) Ai = Creating array of appropriate range && Iterate the frequency array
// N = Filling frequencies of each element

#include<bits/stdc++.h>
using namespace std;

void countSort(int arr[], int n)
{

int k = arr[0];
for(int i=0;i<n;i++)
{
k = max(k,arr[i]);
}

int count[10] = {0};
for(int i=0;i<n;i++)
{
count[arr[i]]++;
}

for(int i=1; i<=k;i++)
{
count[i] += count[i-1];
}

int output[n];
for(int i=n-1; i>=0;i++)
{
output[--count[arr[i]]] = arr[i];
}

for(int i=0;i<n;i++)
{
arr[i] = output[i];
}

}


int main()
{

int arr[] = {1,3,2,3,4,1,6,4,3};
countSort(arr,9);
for(int i=0;i<9;i++)
{
cout << arr[i] << " ";
}

return 0;

}
66 changes: 66 additions & 0 deletions Data-Structures-And-Algorithms-Hacktoberfest18-master/DNF_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Name: JHALA DEVRAJSINH SHRIPALSINH
// Date: 01/07/2021
// Purpose: Dutch National Flag (DNF) Sort
// Approach:
// Check the value of arr[mid]:
// if 0, swap arr[low] and arr[mid], low++, mid++
// if 1, mid++
// if 2, swap arr[mid] and arr[high], high--

// Time complexity:
// O(N) because in each operation, either mid gets incremented or high gets decremented


#include<bits/stdc++.h>
using namespace std;

void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}


void DNF_Sort(int arr[], int n)
{
int low = 0;
int mid = 0;
int high = n - 1;

while(mid <= high)
{
if(arr[mid] == 0)
{
swap(arr,low,mid);
low++; mid++;
}

else if(arr[mid] == 1)
{
mid++;
}

else
{
swap(arr,mid,high);
high--;
}
}
}



int main()
{

int arr[] = {1,0,2,1,0,1,2,1,2};
DNF_Sort(arr,9);

for(int i=0;i<9;i++)
{
cout << arr[i] << " ";
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Name: JHALA DEVRAJSINH SHRIPALSINH
// Date: 01/07/2021
// Purpose: Wave Sort
// Wave form like arr[0]>= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ...

// Time Complexity:
// O(N/2) === O(N)
// Final time complexity === O(N)

#include<bits/stdc++.h>
using namespace std;

void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

void wavesort(int arr[], int n)
{
for(int i=1; i<n; i+=2)
{
if(arr[i] > arr[i-1])
{
swap(arr,i,i-1);
}

if(arr[i] > arr[i+1] && i <= n-2)
{
swap(arr, i,i+1);
}
}
}



int main()
{

int arr[] = {1,3,4,7,5,6,2};
wavesort(arr,7);

for(int i=0;i<7;i++)
{
cout << arr[i] << " ";
}

return 0;
}