c Write a function called mysort that rearranges elements of
c++
Write a function called mysort that rearranges elements of an array a so that they form an increasing sequence of values. Allow for different array positions to contain the same value.
Develop your own sorting algorithm; do not use a predefined sort routine from the standard library.
Implement a simple sorting algorithm rather than an efficient algorithm.
Write code that thoroughly tests your function. Express your tests using assertions.
Solution
// C++ code mysort
#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
using namespace std;
// A function to implement bubble sort
void mysort(int a[], int len)
{
for (int i= 0; i < len-1; i++)
{
// Last i elements are already in place
for (int j = 0; j < len-i-1; j++)
if (a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
int main()
{
int a[] = {23, 32, 11, 89, 12, 27, 80};
int len = sizeof(a)/sizeof(a[0]);
cout << \"UnSorted values: \";
for (int i=0; i < len; i++)
cout << a[i] << \" \";
cout << \"\ \";
mysort(a, len);
cout << \"Sorted values: \";
for (int i=0; i < len; i++)
cout << a[i] << \" \";
cout << \"\ \";
return 0;
}
/*
output:
UnSorted values: 23 32 11 89 12 27 80
Sorted values: 11 12 23 27 32 80 89
*/

