Could someone please explain why my program isn’t sorting the values properly?

For my assignment, we have to build .h file that links the sort.cpp file and the testsort.cpp file. However when I compile and run the testsort.cpp program it doesnt provide the sorted array. Rather it just outputs the original array. I tired to add a return function to the sort.cpp file but i get the following error:

error: return-statement with a value, in function returning 'void' [-fpermissive] return A;

testsort.cpp

#include <iostream>
#include <cstdlib>     

#include "sort.h"


int main() 
    {
      const int n = 10;
      int i, isort;
      float A[n];

      for (i=0;i<n;i++) 
          {
            A[i] = float(rand())/RAND_MAX; 
          }
  
      for (i=0;i<n;i++) 
          {
            std::cout << A[i] << " ";
          }
  
      std::cout << " unsorted\n";

      std::cout << "Enter 1 for insertion sort, 2 for partition test, 3 for quick sort\n";
      std::cin >> isort;

      switch (isort) 
          {
  
              case 1:
              InsertionSort( A, n );
              break;
  
              case 2:
              //  std::cout << "Count for small sub-array " << Partition( A, n ) << "\n";
              break;
  
              case 3:
              //  QuickSort(A,n);
              break;
  
              default:
              std::cout << isort << " is not an allowed choice\n";
          }

      for (i=0;i<n;i++) 
          {
            std::cout << A[i] << " ";
          }
      std::cout << " sorted\n";
    }

Sort.h

#ifndef SORT_H
#define SORT_H

void InsertionSort (float A[], int n){}

#endif

Sort.cpp

#include <iostream>
#include <cmath>
#include "sort.h"

      void InsertionSort (float A[], float n)
      {
        int value;
        int j;
        
        for (int i = 1; i < n; i++)
          {
            value = A[i];
            j = i;
              
              while(j > 0 && A[j-1] > value) 
                {
                   A[j] = A[j-1];
                   j--;
                }
            
            A[j] = value;
          }
         //return A; 
        //std::cout<<"Running Insertion Sort\n";
      }


Read more here: https://stackoverflow.com/questions/64939580/could-someone-please-explain-why-my-program-isnt-sorting-the-values-properly

Content Attribution

This content was originally published by Mohit Singh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: