#What algorithm does this pseudocode perform?

1 messages · Page 1 of 1 (latest)

restive widget
#

I believe it's the insertion sort algorithm although I am not sure

Declare Integer index
Declare Integer scan
Declare Integer unsortedValue
For index = 1 To arraySize - 1
Set unsortedValue = array[index]
Set scan = index
While scan > 0 AND array[scan-1] < array[scan]
Call swap(array[scan-1], array[scan])
Set scan = scan - 1
End While
Set array[scan] = unsortedValue
End For

weary solstice
#

it's like pseudo basic, but not really

#

and didn't I answered that question like last week ?

#

reverse selection sort probably

restive widget
#

oh wow

#

you did answer trhis already lmao

#

my bad lol

#

I meant to send this code instead

weary solstice
#

why don't you try to see how the code execute

restive widget
#

Declare Integer maxElement
Declare Integer index
For maxElement = arraySize - 1 To 0 Step -1
For index = 0 To maxElement - 1
If array[index] > array[index + 1]
Then Call swap(array[index], array[index + 1])
End If
End For
End For

weary solstice
#

or compare it with existing sorting algorithm line by line

#

and see which one matches best

restive widget
#

hmmm

#

alright for this I got bubble sort algorithm

weary solstice
#
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++)
 
        // Last i elements are already in place
        for (j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1])
                swap(&arr[j], &arr[j + 1]);
}
restive widget
#

hmmm

#

thanks

#

again

#

for the assistance

weary solstice
#

close but not quite

#
void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i++)
    {
        key = arr[i];
        j = i - 1;
 
        // Move elements of arr[0..i-1], 
        // that are greater than key, to one
        // position ahead of their
        // current position
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}
 
restive widget
#

ohhh

weary solstice
#
void selectionSort(int arr[], int n)
{
    int i, j, min_idx;
 
    // One by one move boundary of
    // unsorted subarray
    for (i = 0; i < n-1; i++)
    {
       
        // Find the minimum element in
        // unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++)
        if (arr[j] < arr[min_idx])
            min_idx = j;
 
        // Swap the found minimum element
        // with the first element
        if(min_idx!=i)
            swap(&arr[min_idx], &arr[i]);
    }
}
restive widget
#

did you just type that up?

weary solstice
#

googled and copy-paste

restive widget
#

how do you do that?

#

oh

weary solstice
restive widget
#

bros got everything on standby 💀

#

thanks again oh wise one

weary solstice
#

I would say reverse bubble sort maybe?
you would have to run it

restive widget
restive widget
#

she hasn't gone over reverse

weary solstice
#

;compile c

#include <stdio.h>
void swap(int* xp, int* yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
void bubbleSort(int arr[], int n)
{
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1])
                swap(&arr[j], &arr[j + 1]);
}
int main()
{
  return 0;
}
upper baneBOT
#
Compilation successful

No output.

restive widget
#

I had so much homework

#

5 assignments to be exact

#

and I just sat down and finished 4 in less than an hour

#

🙂

#

I got a 90 on the quiz and 100 on the other quiz

weary solstice
#

;compile c

#include <stdio.h>
void swap(int* xp, int* yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
void bubbleSort2(int arr[], int n)
{
    for (int i = n ; i >= 0; i--)
        for (int j = 0; j < i - 1; j++)
            if (arr[j] > arr[j + 1])
{
                swap(&arr[j], &arr[j + 1]);
  printArray(arr, n);
}
}
int main()
{
  int arr[] = { 22, 11, 30, 44, 33, 66, 8, 9, 12 };
  int n = sizeof(arr)/sizeof(int);
  printf("[%d]\n", n);
  bubbleSort2(arr, n);
  printArray(arr, n);
  return 0;
}
upper baneBOT
#
Program Output
[9]
11 22 30 44 33 66 8 9 12 
11 22 30 33 44 66 8 9 12 
11 22 30 33 44 8 66 9 12 
11 22 30 33 44 8 9 66 12 
11 22 30 33 44 8 9 12 66 
11 22 30 33 8 44 9 12 66 
11 22 30 33 8 9 44 12 66 
11 22 30 33 8 9 12 44 66 
11 22 30 8 33 9 12 44 66 
11 22 30 8 9
restive widget
#

gawdd damn

#

I wish I knew how to write code like that

weary solstice
#

it sorted in order, but last digit is not there

#

so it goes up until NOT [left < right] then swap

#

so swap 44/33 and 66/8

#

then 66/9 and 66/12

#

and so on

#

I changed the first loop to go n to 0 instead of n-1 to 0

#

that fixed one bug