A Bubble sort Pseudocode

Hi there.

I am new to this forum and this is my first topic. I have a question in regards to the pseudocode of insertion sort. I am new to programming and currently enrolled in a Computer science degree. Anyway, I find my lecturer's pseudocode very hard to understand. Can anyone explain the following pseudocode to me clearly? I really do not get what is the wallIndex in the pseudocode.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  Algorithm:insertionSort

BEGIN insertionSort (array, N)

      INITIALISE wallIndex to 1
      WHILE (wallIndex <= N-1)

            SET temp to array[wallIndex]
            SET index to wallIndex
            WHILE (index>0 AND array[index-1]>temp)
                  SET array[index] to array[index-1];
                  DECREMENT index
            ENDWHILE
            SET array[index] to temp;
            INCREMENT wallIndex
      ENDWHILE
      RETURN array
END insertionSort
Last edited on
Does this help?
Functionally the wallIndex is the same as the index, they are both pointers which index an item in the array list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
BEGIN insertionSort (array, N)  array is a list of N items
      wallIndex = 1
      WHILE (wallIndex <= N-1)
            temp = array[wallIndex]
            index = wallIndex
            WHILE (index>0 AND array[index-1]>temp)
                  array[index] = array[index-1]
                  index = index - 1
            ENDWHILE
            array[index] = temp
            wallIndex = wallIndex + 1
      ENDWHILE
      RETURN array
END insertionSort


Or maybe it will help if can compare it with a C++ version?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

//using namespace std;

int main()
{
    int array[8];

    array[0]=4;
    array[1]=7;
    array[2]=3;
    array[3]=8;
    array[4]=0;
    array[5]=2;
    array[6]=5;
    array[7]=1;

    for (int i=0;i<8;i++)
    {
        std::cout << array[i];
    }
    std::cout << std::endl;

    int wallIndex = 1;
    while (wallIndex <= 8-1)
    {
        int temp = array[wallIndex];
        int index = wallIndex;
        while (index>0 && array[index-1]>temp)
        {
                array[index] = array[index-1];
                index = index - 1;
        }
        array[index] = temp;
        wallIndex = wallIndex + 1;
    }

    for (int i=0;i<8;i++)
    {
        std::cout << array[i];
    }
    std::cout << std::endl;

    return 0;
}

Last edited on
You are a champion !! Thanks a lot mate..
Topic archived. No new replies allowed.