Thread: Sorting program

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    1

    Sorting program

    Hello,

    I'm trying to write a program that sorts an array of ints.
    I know i can easily find one on internet but I want to know why my program is not working.
    Btw, in this exercice i don't want to use for loop, only while.

    Code:
    #include<unistd.h>
    #include<stdio.h>
    
    void sort(int *arr, int size);
    
    int main(void)
    {
        int ptr[4]; //Initializing the value of the ints
        ptr[0]=2;
        ptr[1]=4;
        ptr[2]=1;
        ptr[3]=3;
        sort(ptr,4); //calling the function
        int i;
        i=0;
    
        while(i<=3)
        {
            printf("%i",ptr[i]); // Printing the modified ptr[]
            i++;
        }
    }
    
    void sort(int *arr, int size)
    {
        int b; //counter
        b=0;
        int temp; //temporary variable for storage
    
        while(b<size)
        {
            if(arr[b]>arr[b+1])
            {
                temp=arr[b];
                arr[b]=arr[b+1]; //swaping values
                arr[b]=temp;
            }
            else
            {
                b++; //If int in order just add 1 to b
            }
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,663
    > Btw, in this exercice i don't want to use for loop, only while.
    while loops and for loops are very similar in C, so switching between them is trivial.

    Code:
    for ( a ; b ; c ) {
      d;
    }
    
    // is approximately
    
    a;
    while ( b ) {
      d;
      c;
    }
    The only sticky part is how break and continue are handled inside each loop construct.

    > but I want to know why my program is not working.
    You normally need two nested loops to implement bubble sort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    8
    if(arr[b]>arr[b+1] => if that's the case, b will never be incremented.
    since you are comparing two neighbouring values, you will also need extra variable, that indicates, if two values were swapped.

    1. before the execution of the while-loop you define an int variable int swapped = 1
    2. while(b < size && swapped == 1) etc
    3. the first statement in the while loop: you set swapped = 0
    4. after you actually swap two variables, you set swapped = 1

    the while loop will be executed as long two variables have been swapped. if no variables have been swapped, all values are already sorted and the program will continue after the while-loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting program
    By semtexs in forum C Programming
    Replies: 23
    Last Post: 11-17-2015, 04:16 PM
  2. Program not sorting
    By mack99 in forum C Programming
    Replies: 1
    Last Post: 11-28-2013, 11:55 PM
  3. help with sorting program
    By pjr5043 in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 03:47 PM
  4. Sorting Program... :(
    By thynksheraze in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2003, 07:01 AM
  5. sorting program
    By jk in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 10:21 PM

Tags for this Thread