-1

I am learning c++ but I am following a terrible book which was recommended to me at school. It has many errors but I found a blunder where the logic of the program is wrong. Could you please check if this is really a an error or I am understanding it wrong?

According to the book code for bubble sort is as follows -

#include <iostream.h>
main()
{
  int num, i, j, temp;
  cin >> num;
  int item[num];
  for (i = 0; i < num; i++)
    cin >> item[i];
  for (i = 0; i < num -1; i++)
    for ( j = i+1; j < num; j++)
      if (item[i] > item[j]){
         temp = item[i];
         item[i] = item[j];
         item[j] = temp;
      }

what I think - I think that this is linear sort and not bubble sort because here we are comparing one element to all the elements below it in the array and if a smaller element is found swapping them.

Accourding to me the correct code for bubble sort should be like

void bubble_sort_bottomsup(int a[], int n)
{
  //implementing bubble sort
  //a[] is the array to be sorted and n is the total number of elements in that array
  for (int i = 1; i<n; i++)
  {
     for (int j = n-1; j>0; j--)
     {
        if (a[j] < a[j-1])
        {
            int temp;
            temp = a[j-1];
            a[j-1] = a[j];
            a[j] = temp;
        }
     }
  }
}

is what I am saying correct?? I am new to c++ and this book a followed widely in my country so not so sure that there could really be a logical flaw in the book's code. And teachers at my school also keep on saying that the code given in the book for bubble sort is correct.

So please help me out.

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
  • Are you sure the first block of code is correct? It doesn’t seem to close the { from main. –  Sep 02 '18 at 06:16
  • i did not complete that block as I just wanted to show the sorting part. – Nitin Pathak Sep 02 '18 at 06:18
  • 3
    `#include main()` -- That book must be so dog-eared with yellowing pages. Code like this has been obsoleted for 20 years now. – PaulMcKenzie Sep 02 '18 at 06:23
  • Bubble sort is supposed to swap adjacent pairs. The first code doesn't do that. – PaulMcKenzie Sep 02 '18 at 06:25
  • @n.m Things won't probably change until the books the students are given literally fall apart when opened. – PaulMcKenzie Sep 02 '18 at 06:30
  • @PaulMcKenzie No worries they will gist give out PDFs. – n. m. could be an AI Sep 02 '18 at 06:31
  • Not just ``, but also VLA (variable-length arrays), no `std` namespace, declaring all variables at the top, and not using `std::swap`. This code is before 1998 when C++ was standardized. And that all before considering that this is not bubble sort. – Michael Veksler Sep 02 '18 at 06:40
  • @NitinPathak the reason I point it out is that you seem to be asking us to compare & contrast the book’s code with yours. –  Sep 02 '18 at 06:54
  • Any recommendations for a good book for learning modern c++, I have covered the basics pretty much from my present book so I think I will enjoy something a little (not much) advanced.. – Nitin Pathak Sep 02 '18 at 07:16
  • What's the book? – john Sep 02 '18 at 07:52

1 Answers1

3

The first code block is not bubble-sort because bubble-sort swaps adjacent elements. "Linear sort" is a non-standard and confusing term, different people use this name for different things and I recommend against using it. There's no entry for "linear sort" in Wikipedia. The algorithm in the first block is commonly called selection sort.


Not directly related to your question: the book (and unfortunately the entire education system of at least one very large country) is using a horribly outdated dialect of C++. The language has changed very significantly since its advent ca 1983. As of now, the original dialect can only be found in very few very specialised niches. For learning modern C++, see StackOverflow's own The Definitive C++ Book Guide and List.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243