Home > Software engineering >  Need assistance regarding desired output
Need assistance regarding desired output

Time:11-23

Assigned Question: Given the unsorted list [6, 5, 4, 3, 7, 1, 2], show what the contents of the list would be after each iteration of the loop as it is sorted using the Selection sort in Python.

My code in Python:

Arr = [6, 5, 4, 3, 7, 1, 2]

print("\n")

for i in range (len(Arr)):
    min_idx = i
    for j in range (i 1, len(Arr)):
        if Arr[min_idx] > Arr[j]:
            min_idx = j

    Arr[i], Arr[min_idx] = Arr[min_idx], Arr[i]
    print("Loop is at index = {0}. Sorting in progress: ".format(i), end=' ')
    print(Arr)


print("\nSorted array: ", Arr)

And this is the output of the above program:

Loop is at index = 0. Sorting in progress:  [1, 5, 4, 3, 7, 6, 2]
Loop is at index = 1. Sorting in progress:  [1, 2, 4, 3, 7, 6, 5]
Loop is at index = 2. Sorting in progress:  [1, 2, 3, 4, 7, 6, 5]
Loop is at index = 3. Sorting in progress:  [1, 2, 3, 4, 7, 6, 5]
Loop is at index = 4. Sorting in progress:  [1, 2, 3, 4, 5, 6, 7]
Loop is at index = 5. Sorting in progress:  [1, 2, 3, 4, 5, 6, 7]
Loop is at index = 6. Sorting in progress:  [1, 2, 3, 4, 5, 6, 7]

Sorted array:  [1, 2, 3, 4, 5, 6, 7]

Now, I am trying to do a the same sorting but with the technique of Insertion Sort and in C Language. But, I am not getting the output like the above written Python program.

My code in C:

#include <stdio.h>

void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; 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;

        // to print the sorting in progress
        for (j = 0; j < n; j  ){
            // printf("Loop is at index = %d. ", j);
            printf("%d ", arr[j]);
            if(j == n - 1)
                printf("\n");
        }
    }
}

void printArray(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i  )
        printf("%d ", arr[i]);
    printf("\n");
}

int main()
{
    int arr[] = {6, 5, 4, 3, 7, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("\nOriginal array: ");
    printArray(arr, n);

    printf("\nSorting in progress: \n\n");
    insertionSort(arr, n);
    printf("\n");

    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}

The output I am getting right now:

Original array: 6 5 4 3 7 1 2 

Sorting in progress: 

5 6 4 3 7 1 2 
4 5 6 3 7 1 2 
3 4 5 6 7 1 2 
3 4 5 6 7 1 2 
1 3 4 5 6 7 2 
1 2 3 4 5 6 7 

Sorted array: 1 2 3 4 5 6 7 

How do I add the following line in my output of the C program?

Loop is at index = indexValue

I tried but I couldn't figure it out.

CodePudding user response:

If you want to print the debug output for each iteration, move the print statement to the outermost for loop (the i loop). Currently where you have added the debug in the code, it will print "Loop is at index" for each element of the array.

for (i = 1; i < n; i  ) {
        printf("Loop is at index = %d. ", i);
        ....
}

CodePudding user response:

Looking at your code I see that you tried to use the printf() statement in a inner for loop (for loop inside a for loop). This will result in the output apearing 7 * 6 times:

Loop is at index = 0. 4 Loop is at index = 1. 5 Loop is at index = 2. 6 Loop is at index = 3. 3 Loop is at index = 4. 7 Loop is at index = 5. 1 Loop is at index = 6. 2
Loop is at index = 0. 3 Loop is at index = 1. 4 Loop is at index = 2. 5 Loop is at index = 3. 6 Loop is at index = 4. 7 Loop is at index = 5. 1 Loop is at index = 6. 2
Loop is at index = 0. 3 Loop is at index = 1. 4 Loop is at index = 2. 5 Loop is at index = 3. 6 Loop is at index = 4. 7 Loop is at index = 5. 1 Loop is at index = 6. 2
Loop is at index = 0. 1 Loop is at index = 1. 3 Loop is at index = 2. 4 Loop is at index = 3. 5 Loop is at index = 4. 6 Loop is at index = 5. 7 Loop is at index = 6. 2
Loop is at index = 0. 1 Loop is at index = 1. 2 Loop is at index = 2. 3 Loop is at index = 3. 4 Loop is at index = 4. 5 Loop is at index = 5. 6 Loop is at index = 6. 7

To limit the amount of times you see the output you need to put the printf() statement in the outer for loop (the first one). Just a copy and paste you will see that the printf() statement is only ran 6 times:

Loop is at index = -1. 5 6 4 3 7 1 2
Loop is at index = -1. 4 5 6 3 7 1 2
Loop is at index = -1. 3 4 5 6 7 1 2
Loop is at index = 3. 3 4 5 6 7 1 2
Loop is at index = -1. 1 3 4 5 6 7 2
Loop is at index = 0. 1 2 3 4 5 6 7

This looks like the output you are going after, but you still need to figure out variable to put in the printf() statement.

CodePudding user response:

How do I add the following line in my output of the C program?

Loop is at index = indexValue

It is very similar in concept to what you have done in the Python language. With special attention payed to where in respect to a loop you place it, in addition to using (or not using) the newline (\n) character...

In the following adaptation of your code, I moved the printf statements around, and removed a few newlines, then added a few more. See comments in line to see changes. This should get you close to the Python code, and in addressing your other stated goals:

void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i  ) {
        printf("Loop is at index = %d. ", i);//moved from sort
        
        key = arr[i];
        j = i - 1;
        printf(" Sorting in progress: ");//moved from main
        while (j >= 0 && arr[j] > key) {
            arr[j   1] = arr[j];
            j = j - 1;
        }
        arr[j   1] = key;

        // to print the sorting in progress
        printf("[");//added here
        for (j = 0; j < n; j  ){
            if(j < n-1)//changed - if-else
                
                printf("%d ", arr[j]);
            else
                printf("%d", arr[j]);

                
        }
        printf("]\n");//move outside loop
    }
}

void printArray(int arr[], int n)
{
    int i;
    
    printf("[");//changed
    for (i = 0; i < n; i  )
        printf("%d ", arr[i]);
    printf("]\n");//changed
}

int main()
{
    int arr[] = {6, 5, 4, 3, 7, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("\nOriginal array: ");
    printArray(arr, n);

    //printf("\nSorting in progress: \n\n");
    insertionSort(arr, n);
    printf("\n");

    printf("Sorted array: ");//changed
    printArray(arr, n);
    
    printf("\n\nHit any key to exit,");//added to pause execution
    getchar();

    return 0;
}

Output from this code:
enter image description here

  • Related