0

I want to use (sort insert) in letters, not numbers, but I didn't understand how I wrote this code using the (Insertion sort array).

   #include <bits/stdc++.h> 
using namespace std; 

void insertionSort(int arr[], int n)  
{  
    int i, key, j;  
    for (i = 1; n - 1; i++) 
    {  
        key = arr[i];  
        j = i - 1;  

        while (j >= 0 && arr[j] > key) 
        {  
            arr[j + 1] = arr[j];  
            j = j - 1;  
        }  
        arr[j + 1] = key;  
    }  
}  


void printArray(int arr[], int n)  
{  
    int i;  
    for (i = 0; i < n; i++)  
        cout << arr[i] << " ";  
    cout << endl; 
}  

But there is an error here

    int main()  
{  
    char s[] = { "Q, R, Y, M, N, L, O" };
    int n = sizeof(arr) / sizeof(arr[0]);

    insertionSort(arr, n);  
    printArray(arr, n);  

    return 0;  
}  
  • "But there is an error here" - what's the error? – ForceBru May 13 '20 at 19:02
  • What is your error? Keep in mind that the `char s[]` contains 20 characters: `'Q', ',', ' ', 'R', ',', ' ', ..., 'O', '\0'`. You're also trying to take the `sizeof(arr)`, when `arr` isn't declared. What's the exact error you're seeing? – JohnFilleau May 13 '20 at 19:04
  • 1
    We're also aware that you didn't write this original code. It's fine. Taking pieces of other people's code is okay and we won't crucify you for it. But you can be honest here about that. – JohnFilleau May 13 '20 at 19:05
  • 1
    Your `insertionSort` doesn't work, and your `main` doesn't compile. You must work on simpler exercises a while longer, until you master the basics. – Beta May 13 '20 at 19:09
  • 1
    If your compiler is up to date you'll have access to `std::size`, and that sucker can get you the size of pretty much anything that still has a known size. – user4581301 May 13 '20 at 19:09
  • How did you know that I didn't write it? I study this in a textbook – Hussam Taha May 13 '20 at 19:12
  • 1
    Any textbook that starts code samples with `#include ` and `using namespace std;` should be burned and its ashes buried on consecrated ground. – user4581301 May 13 '20 at 19:14
  • 1
    any textbook that uses this prototype should be burned: `void insertionSort(int arr[], int n) ` – bolov May 13 '20 at 19:19
  • I learned (Insertion sort array) from the book but #include using namespace std; learned it myself – Hussam Taha May 13 '20 at 19:19
  • Do anyone help me or not? – Hussam Taha May 13 '20 at 19:22
  • 1
    Praise Crom. I recommend you unlearn it. anything inside the bits folder is implementation specific for the g++ compiler and not intended for direct inclusion. Weird things can and do happen. In this case there is little weird, but it includes the entire C++ standard library and that's a lot of extra work for the compiler and tens of thousands of extra symbols running loose inside your program you now have to avoid. – user4581301 May 13 '20 at 19:23
  • 1
    Additional reading on the topic: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 May 13 '20 at 19:26
  • Ok, Is all code wrong? – Hussam Taha May 13 '20 at 19:34

2 Answers2

0

Your character array is named s, but you tried to calculate its length assuming the name of the array is arr

Fix the array name:

int main()  
{  
    char arr[] = { "Q, R, Y, M, N, L, O" };
    int n = sizeof(arr) / sizeof(arr[0]);

    insertionSort(arr, n);  
    printArray(arr, n);  

    return 0;  
}
kwsp
  • 1,184
  • 7
  • 26
  • @HussamTaha Make sure the error is the same error. You're going to find that it's easy to have multiple errors and for one error to conceal others. Compilers have gotten better, but when I was learning fixing one error often lead to the revelation of hundreds more because the compiler could now process further. – user4581301 May 13 '20 at 19:18
0

I did small fixes in your code

void insertionSort(char arr[], int n) // coz you pass the array of char

and

for (i = 1; i < n; i++) // coz loop should be till the end. not n - 1 condition

and

void printArray(char arr[], int n) // coz u pass array of char type

and finally

int main()  
{  
    char s[] = { 'Q', 'R', 'Y', 'M', 'N', 'L', 'O' }; // read about initializin arrays and designated initializers if u need it
    int n = sizeof(s) / sizeof(s[0]); // arr replaced with s

    insertionSort(s, n);  // arr replaced with s
    printArray(s, n);   // arr replaced with s

    return 0;  
}

As people mentioned here you should start from the basics first. Try to avoid copy people's code until u understand what happens while execution of this code.

Insertion sort more details

Sn0w1eo
  • 56
  • 3