Commit 7083cfda authored by RAMYA NARAYANASAMY's avatar RAMYA NARAYANASAMY

change implementation to insertion-sort

parent 838eab18
#include "sorting.h"
vector<int> sort_custom(vector<int> a)
{
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;
}
}
}
return a;
vector<int> sort_custom(vector<int> arr){
int i, key, j;
for (i = 1; i < arr.size(); i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
return 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