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

Added Merge sort #1

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open

Conversation

siddharthnarula
Copy link

#include
#include<conio.h>
#include<dos.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */
int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
    L[i] = arr[l + i];
for (j = 0; j < n2; j++)
    R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
    if (L[i] <= R[j])
    {
        arr[k] = L[i];
        i++;
    }
    else
    {
        arr[k] = R[j];
        j++;
    }
    k++;
}

/* Copy the remaining elements of L[], if there
   are any */
while (i < n1)
{
    arr[k] = L[i];
    i++;
    k++;
}

/* Copy the remaining elements of R[], if there
   are any */
while (j < n2)
{
    arr[k] = R[j];
    j++;
    k++;
}

}

/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{ Sleep(1000);
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

    // Sort first and second halves
    mergeSort(arr, l, m);
    mergeSort(arr, m+1, r);

    merge(arr, l, m, r);
}

}

void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
clock_t start,end;
int arr[100],n=25;
for(int i=0;i<n;i++)
{
arr[i] = (rand()%100+1);
cout << arr[i] << endl;
}
//int n = sizeof(arr)/sizeof(arr[0]);
start=clock();
mergeSort(arr,0,n);
end=clock();
cout<<"Sorted array: \n";
printArray(arr, n);
float t=(end-start)/CLK_TCK;
cout<<"\n Time Taken is"<<t;
return 0;
}

Vishal Bhardwaj and others added 30 commits October 5, 2018 04:42
* Added Reverse_Queue.cpp

* Update stack.hpp
i am adding merge sort coded in java.
* Create Kosaraju_algorithm

The Kosaraju algorithm is an important graph algorithm to detect strongly connected components in a graph. A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. This algorithm is often the first step in solving many graph questions.

* Create Dynamic_fibonacci.py
* Create square_root_decomposition

* Create travelling_salesman_problem

* Add files via upload
* Add Segment Tree Java

* Add Segment Tree with Lazy Propagation Java

* Add Binary Search Tree Java
* Create createBST.cpp

* Create bucketsort.c
To find the longest increasing subsequence in an array of integers.
* Adding Linear/Binary search in java packages

* Adding further instructions in TODO file.
* OS scheduling algorithms

* OS scheduling algorithms
* Create bubblesort.cpp

* Create Stack_as_linked_list.cpp
aanchal19 and others added 13 commits October 9, 2018 15:36
…cheema1#169)

* Create bfs.cpp

* Create BFS.py

* Rename bfs.cpp to BFS.cpp

* Update BFS.cpp

This is a Breadth First Search Code in C++ language. An exemplary graph is made in the main() function and starting is done from 2nd vertex.(can be changed as per the requirement).

* BFS Algorithm added to graph folder
* added an efficient median search algo

* btree code added
Full code of merge sort in python3 with an example. Also provided the time and space complexity.
* added quick sort in python3

* binary search in python3
Greedy choice property used : Take the item which gives maximum profit whenever possible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.