bubble sort

+1 Baba Iskandarov · December 30, 2015
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>


int main()
{
    int i, temp, swapped;
    int howMany;
    int goals[howMany];

    printf("How many numbers do you wanna sort?\n");
    scanf(" %d", &howMany);
    printf("random numbers\n");

    for (i = 0; i < howMany; i++) {
        goals = (rand()%26) + 1;
        printf("%d \n", goals);
    }
    while(1) {
        swapped = 0;
        for (i = 0; i < howMany - 1; i++) {
            if (goals > goals) {
                int temp = goals;
                goals = goals;
                goals = temp;
                swapped = 1;
            }
        }
        if (swapped == 0) {
            break;
        }
    }
    printf("\nSorted numbers\n");
    for (i = 0; i < howMany; i++) {
        printf("%d\n", goals);
    }
    return 0;
}


Why its not working?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Michael Bianchi · January 10, 2016
I probably should have given you a C-specific example, but the point I was making was that you can use the principle outline here and apply it in C without much of a syntax change.

The part of most interest to you would be here:

void bubbleSort::Sort(){

for(i = 0; i < n; i++){ // until array has been iterated;

for(j = 0; j < n-i-1; j++){ // j is the storage for the next item to be sorted;

if(a[j] > a[j + 1]) // if array iterates through 'j' and is greater than an element from ('j' + 1) then...
{

int t = a[j]; // t = iteration of array through 'j';
a[j] = a[j + 1]; // iteration of array through 'j' = the iteration + 1;
a[j + 1] = t; // a iterated through j + 1 now equals 't';

}
}

}

}

#SneakySnake
+1 Baba Iskandarov · December 31, 2015
Michael Bianchi thank you Michael. but im struggling with c i dont know anything about c++:D
0 Baba Iskandarov · December 31, 2015
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>


int main()
{
    int i, temp, swapped;
    int howMany;
    int goals[howMany];

    printf("How many numbers do you wanna sort?\n");
    scanf(" %d", &howMany);
    printf("random numbers\n");

    for (i = 0; i < howMany; i++) {
        goals = (rand()%26) + 1;
        printf("%d \n", goals);
    }
    while(1) {
        swapped = 0;
        for (i = 0; i < howMany - 1; i++) {
            if (goals > goals) {
                int temp = goals;
                goals = goals;
                goals = temp;
                swapped = 1;
            }
        }
        if (swapped == 0) {
            break;
        }
    }
    printf("\nSorted numbers\n");
    for (i = 0; i < howMany; i++) {
        printf("%d\n", goals);
    }
    return 0;
}

Thank you for repliing me. 
here the code its get 10 numbers and doesnt sort it sorry for my english.
+1 Michael Bianchi · December 31, 2015
Oh God, not C :)

It looks to me like you're asking it to compare between two int variables instead of an array of variables. For bubble sorting to work properly, you probably should compare it to an array of variables and then store the current variable inside a temporary one like this, in theory:

Here is a bubble sort program I wrote in c++ years ago, should suit you well. Be advised, it is an object oriented version but all principles apply for you. It is heavily commented for you.


===========bubbleSort.h

#pragma once
#include <stdlib.h>
#include <conio.h>
#include <iostream>

class bubbleSort
{
public:

    // The bubble sort class is very simple. There are only 3 functions and 5 variables;

    bubbleSort(void);
    ~bubbleSort(void);

    void DisplayResults(); // Prints to screen the results;
    void Sort(); // Sorts user-entered elements;
    void Read(); // Takes in user-entered elements;
    

private:

    int i,j,n,a[20]; // 'a' is the array that will store user-entered elements; i, j, and n are loop integers;

};

============bubbleSort.cpp

#include "stdafx.h"
#include "bubbleSort.h"


bubbleSort::bubbleSort(void)
{
    i = NULL;
    n = NULL; // Primary ints are initialized to NULL;
    j = NULL;

}


bubbleSort::~bubbleSort(void)
{
    // Deconstructor
}

void bubbleSort::Read(){

    cout << "Enter the number of elements: ";
    cin >> n; // Stores a user-specific number of elements, up to over 32,000 positive numbers in this case.

    cout << "Enter the array elements: " << "\n";
    
    for(i = 0; i < n; i++){ // start at array element 1 ([0]) and enter until the elements amount to the number of user elements;
    
        cin >> a; // a iteration of user input
        cout << "\n";

    }

    cout << endl;

}

void bubbleSort::Sort(){

    for(i = 0; i < n; i++){ // until array has been iterated;
    
        for(j = 0; j < n-i-1; j++){ // j is the storage for the next item to be sorted;
        
            if(a[j] > a[j + 1]) // if array iterates through 'j' and is greater than an element from ('j' + 1) then...
            {
            
                int t = a[j]; // t = iteration of array through 'j';
                a[j] = a[j + 1]; // iteration of array through 'j' = the iteration + 1;
                a[j + 1] = t; // a iterated through j + 1 now equals 't';

            }
        }
    
    }

}

void bubbleSort::displayResults(){

    cout << "\n" << "The elements have been sorted. They are as follows: \n\n";
    for(i = 0; i < n; i++){
    
        cout << "| " << a << " |   ";

    }

    cout << endl;

}

// Bubble Sort.cpp :  the entry point for the console application.

#include "stdafx.h"
#include "bubbleSort.h"

int main()
{
    bubbleSort* bs = new bubbleSort();

    bs->Read(); // Class member function that reads user input
    bs->Sort(); // Sorts user input
    bs->displayResults(); // Displays user input

    _getch(); // this halts the program if I remember, you to not need this

    return 0;
}
  • 1

C

107,220 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator