Commit ebe6a3b5 authored by KSHITIJ SINGHA's avatar KSHITIJ SINGHA

Merge branch 'merge-sort'

Conflicts:
	Makefile
	a.out
	main.cpp
	sorting.cpp
	sorting.h
parents 58ca7897 e75ce9e4
<<<<<<< HEAD
all: hello
hello: main.o sorting.o
......@@ -12,3 +13,18 @@ sorting.o: sorting.cpp
clean:
rm -rf a.out
=======
CC = g++
all: main.o sorting.o
g++ main.o sorting.o -o main
main.o: main.cpp sorting.h
g++ -I . -c main.cpp
sorting.o: sorting.cpp sorting.h
g++ -I . -c sorting.cpp
clean:
rm -rf *.o
>>>>>>> merge-sort
......@@ -3,34 +3,36 @@
#include "sorting.h"
using namespace std;
<<<<<<< HEAD
vector<int> sort_custom(vector<int>& a);
void printVector(vector<int> a);
=======
vector<int> sort_custom(vector<int>);
>>>>>>> merge-sort
int main(int argc, char const *argv[])
{
vector<int> a;
int num = 0;
while (num != -1)
{
std::cin>>num;
if (num != -1)
//add elements to the vector container
a.push_back(num);
}
<<<<<<< HEAD
//printVector(a);
a = sort_custom(a);
=======
>>>>>>> merge-sort
printVector(a);
}
void printVector(vector<int> a)
{
for (int i=0; i <a.size(); i++)
a = sort_custom(a);
for (int i=0; i <a.size(); i++)
{
cout<<a[i]<<" ";
}
}
<<<<<<< HEAD
#include <vector>
#include "sorting.h"
using namespace std;
......@@ -16,4 +17,59 @@ vector<int> sort_custom(const vector<int>& a)
}
}
return a;
=======
#include <iostream>
#include <vector>
using namespace std;
vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
>>>>>>> merge-sort
}
vector<int> sort_custom(vector<int> m)
{
if (m.size() <= 1)
return m;
vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;
for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}
for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}
left = sort_custom(left);
right = sort_custom(right);
result = merge(left, right);
return result;
}
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
using namespace std;
<<<<<<< HEAD
vector<int> sort_custom(vector<int>& a);
void printVector(vector<int> a);
=======
vector<int> sort_custom(vector<int>);
>>>>>>> merge-sort
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment