Jump to content

So this isn't the first time i'm using "bubble sort" method, but for some reason it crashes the console when executed, maybe you guys can see the error.

Even when it's not in a function it crashed( just bear it in mind).

void sort(int n, int tom[]){    for(int i=0; i<n-1; i++)    {        for(int j=i+1; i<n; j++)        {            if(tom[j]<tom[i])            {                int s=tom[i];                tom[i]=tom[j];                tom[j]= s;            }        }    }}

EDIT: i see in the second loop i used i instead of j :D

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/459134-c-sort-error/
Share on other sites

Link to post
Share on other sites

This is a quick and slightly optimized bubble sort function I got from my sorting algorithms thread :

void bubblesort(int* ar, int ar_length){    bool is_sorted;    int k = 1;    do    {        is_sorted = true;        for(int i = 0; i < ar_length-k; i++)            if(*(ar+i) > *(ar+i+1))            {                is_sorted = false;                swap(arr, i , i+1);            }        k++;    }    while(!is_sorted);}

Where swap() is :

void swap(int *arr, index1, index2){int aux = *(arr + index1);*(arr + index1) = *(arr + index2);*(arr + index2) = aux;}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/459134-c-sort-error/#findComment-6161261
Share on other sites

Link to post
Share on other sites

 

This is a quick and slightly optimized bubble sort function I got from my sorting algorithms thread :

void bubblesort(int* ar, int ar_length){    bool is_sorted;    int k = 1;    do    {        is_sorted = true;        for(int i = 0; i < ar_length-k; i++)            if(*(ar+i) > *(ar+i+1))            {                is_sorted = false;                swap(arr, i , i+1);            }        k++;    }    while(!is_sorted);}

Where swap() is :

void swap(int *arr, index1, index2){int aux = *(arr + index1);*(arr + index1) = *(arr + index2);*(arr + index2) = aux;}

btw if i have an array tom[amount] (we dont know the amount), what function should i use to find out the quantity? size(), lenght()?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/459134-c-sort-error/#findComment-6161442
Share on other sites

Link to post
Share on other sites

btw if i have an array tom[amount] (we dont know the amount), what function should i use to find out the quantity? size(), lenght()?

You don't.

C/C++ doesn't manage arrays lengths.

You can, however, create a specially-terminated integer array. (that's how c-style strings work). Or maybe create a struct :

 

typedef struct t_thing {  int* things;  size_t count;} t_thing;

Or use std::vector.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/459134-c-sort-error/#findComment-6162092
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×