1
/*Bubble sort*/
#include <iostream>
using namespace std;
//function to sort the array
int bubble_sort (int k[], int n){
     int t,ct=1;
     while(ct<n){
           for(int i=0;i<n-ct;i++){
             if(k[i]>=k[i+1]){
               t=k[i];
               k[i]=k[i+1];
               k[i+1]=t;
              }
             ct++;
           }
      }
      ///loop to o/p the sorted array
             for(int i=0;i<n;i++){
                 cout<<k[i]<<" ";
             }cout<<endl;    
      return 0;
     }

int main(){
    int l; 
    int r[l];
    cout<<"Enter the array size"<<endl;
    cin>>l;
     cout<<"Enter elements"<<endl;
     for(int i=0;i<l;i++){
        cin>>r[i];
     }
   bubble_sort(r,l);
    return 0;

}
/*Insertion Sort*/

#include <iostream>
using namespace std;
//function to sort the array
int insertion_sort (int k[], int n){
     int t,ct=1;
         
           for(int i=1;i<n;i++){
            int current=k[i];
            int j=i-1;
               while(k[j]>current && j>=0){
                 k[j+1]=k[j];
                 j--;
               }
               k[j+1]=current;
          }
     
               for(int i=0;i<n;i++){
                 cout<<k[i]<<" ";
             }cout<<endl;    
      return 0;
     }

int main(){
    int l; 
    int r[l];
    cout<<"Enter the array size"<<endl;
    cin>>l;
     cout<<"Enter elements"<<endl;
     for(int i=0;i<l;i++){
        cin>>r[i];
     }
   insertion_sort(r,l);
    return 0;
}

The images attached show the output that I get in the terminal(which is blank) when I run the respective codes. The first algorithm illustrates the bubble sorting algorithm and the 2nd is meant to implement insertion sorting technique. Any help in this regard is much appreciated!

Output in the terminal for bubble sort code

Output in the terminal for bubble sort code

Output in the terminal for the insertion sort code

Output in the terminal for the insertion sort code

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 1
    Your programs have undefined behaviour, and may or may not appear to work. When you write `int r[l];`, the array is not magically resized by a wizard when you later decide what `l` is. – molbdnilo Jan 02 '21 at 05:51
  • Your program never does what you want, but what you tell it to do ;) VLAs like `r[l]` are also [not part of the C++ standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard), use `std::vector` instead. – Lukas-T Jan 02 '21 at 07:44

1 Answers1

0

This part of your main contains typical C++ newbie errors.

int l; 
int r[l];
cout<<"Enter the array size"<<endl;
cin>>l;

When you write int l, you only reserve some memory and give a nice name l to it. The value of l is arbitrary, or rubbish. Then you write int r[l]. This has several problems in it. First, the C++ language does not allow for such array definitions unless l is a constant whose value is known to the compiler. So this is an error, but many compilers do not flag it as an error, because they magically allow this particular departure from the standard and call it "extension" (VLA). The price for it is that your program is not standard and its behavior may be compiler-dependent. Second, as you already know, the value of l is undefined. Thus, any execution of your program may result in a different output, including correct (expected) behavior or even a sudden death (crash).

What yoo ned to do is this:

  • declare a variable that will store the array size. Give to it a meaningful name, like size, avoid names like l, as they are sometimes difficult to tell from 1 and I.
  • read its value from std::cin
  • construct an array of size size. Do not use the int a[size] syntax, for you already know it is not permitted by the standard. Use std::vector.

If after these changes your program still behaves in a strange way, call for help again.

Also, before you ask for help again, please include these flags to gcc:

-Wall -pedantic

They will issue lots of diagnostic information if your program does not adhere to usual coding standards (-Wall) or the C++ standard (-pedantic). Work on your source code until you can see no errors and no warnings.

zkoza
  • 2,644
  • 3
  • 16
  • 24