the array index could be -1 #40

Closed
opened 2026-01-29 15:01:34 +00:00 by claunia · 3 comments
Owner

Originally created by @VincentGogo on GitHub (Jan 9, 2020).

807abcd0e8/sorting/gnome_sort.c (L9)

Originally created by @VincentGogo on GitHub (Jan 9, 2020). https://github.com/TheAlgorithms/C/blob/807abcd0e8b644f909452f53b422c8288d8a2554/sorting/gnome_sort.c#L9
Author
Owner

@StepfenShawn commented on GitHub (Apr 18, 2020):

Hi @VincentGogo !
This is gnome_sort's pseudo code:

procedure gnomeSort(a[]):
    pos := 0
    while pos < length(a):
        if (pos == 0 or a[pos] >= a[pos-1]):
            pos := pos + 1
        else:
            swap a[pos] and a[pos-1]
            pos := pos - 1

But you said that the array index can be - 1, I don't quite understand why to do this,Could you provide the running result after modifying the code?
Thank you!

@StepfenShawn commented on GitHub (Apr 18, 2020): Hi @VincentGogo ! This is gnome_sort's pseudo code: ``` procedure gnomeSort(a[]): pos := 0 while pos < length(a): if (pos == 0 or a[pos] >= a[pos-1]): pos := pos + 1 else: swap a[pos] and a[pos-1] pos := pos - 1 ``` But you said that the array index can be - 1, I don't quite understand why to do this,Could you provide the running result after modifying the code? Thank you!
Author
Owner

@VincentGogo commented on GitHub (Apr 18, 2020):

@StepfenShawn The pseudo code is right, The C code at line 9 should be
if ( pos == 0 || numbers[pos] >= numbers[pos-1]

@VincentGogo commented on GitHub (Apr 18, 2020): @StepfenShawn The pseudo code is right, The C code at line 9 should be `if ( pos == 0 || numbers[pos] >= numbers[pos-1]`
Author
Owner

@StepfenShawn commented on GitHub (Apr 19, 2020):

OK, I see. I'll fix it soon. Thank you!

@StepfenShawn commented on GitHub (Apr 19, 2020): OK, I see. I'll fix it soon. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/C#40