Commit f544005f authored by DIPTYAROOP MAJI's avatar DIPTYAROOP MAJI

change implementation to insertion-sort

parent b1e0aaa8
a.out 100644 → 100755
No preview for this file type
......@@ -4,7 +4,8 @@
using namespace std;
void bubbleSort(vector<int>& a);
//void bubbleSort(vector<int>& a);
vector<int> sort_custom(vector<int> arr);
void printVector(vector<int> a);
......@@ -20,7 +21,7 @@ int main(int argc, char const *argv[])
a.push_back(num);
}
//printVector(a);
bubbleSort(a);
a = sort_custom(a);
printVector(a);
......
#include <vector>
#include "sorting.h"
using namespace std;
#include <vector>
void bubbleSort(vector<int>& a)
vector<int> sort_custom(const vector<int> arr)
{
bool swapp = true;
while(swapp){
swapp = false;
for (size_t i = 0; i < a.size()-1; i++) {
if (a[i]>a[i+1] ){
a[i] += a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] -=a[i+1];
swapp = true;
}
unsigned int i, j;
vector<int> sortedArray(arr.size());
sortedArray = arr;
int max;
for(i = 0; i<arr.size();i++){
//max = arr[i];
for(j = i; j<arr.size();j++){
if(sortedArray[i] > sortedArray[j]){
int tmp;
tmp = sortedArray[i];
sortedArray[i] = sortedArray[j];
sortedArray[j] = tmp;
}
}
}
// return a;
return sortedArray;
}
#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;
void bubbleSort(vector<int>& a);
//void bubbleSort(vector<int>& a);
void printVector(vector<int> a);
vector<int> sort_custom(vector<int> arr);
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