Jump to content

Okay I've been given a bubblesort function and i've wrote a main program to use bubblesort. However I also have to write a swap function, I think i understand what to put inside the swap function body, but i'm not sure what paramters to put in the swap function. I'll show you the code. I've commented what needs to be done.

 

#include <stdio.h>
#include <stdlib.h>

void SWAP ()//I don't know what paramtaers to put in the swap function

void bubblesort (int a[], int N)
{
     int i, j;
     
     for (i = N - 1; i > 0; i--)
         for (j = 0; j < i; j++)
             if (!(a[j] <= a[j+1]))
                SWAP(a,j,j+1);//a swap function for here, I'm not sure how this part works        
}

int main()
{
	int array[], number, i, j;
	
	printf("Enter the number of elements: ");
	scanf("%i", &number);
	
	prinf("You can now enter %i elements: ", number)
	
    for (i = 0; i < number: i++;)
        scanf("%i", array[i]);
	
	bubblesort(array, nuumber);
	
	for (j  = 0; j < number; j++ )
        printf("%ld\n", array[c]);
 
    return 0;   
}

 

   
   
Link to comment
https://linustechtips.com/topic/678770-c-writing-a-swap-function-for-bubblesort/
Share on other sites

Link to post
Share on other sites

It's just the function that swaps the two elements your sort selected, all you need to do is to save one of the elements in a buffer, copy the second element where the first was and then copy the buffer where the second was.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

14 minutes ago, Sauron said:

It's just the function that swaps the two elements your sort selected, all you need to do is to save one of the elements in a buffer, copy the second element where the first was and then copy the buffer where the second was.

I know that part I'm just unsure how I should layout the parameters in the swap function declaration.

   
   
Link to post
Share on other sites

Generally, one provides pointers to the actual elements to swap to a swap function so you don't have to pass along the array itself.

 

void SWAP(int *a, int *b)
{
	int Temp = *a;	
  	*a = *b;
  	*b = Temp;
}

Then you can call it as such:

SWAP(a + j, a + j + 1);

Wich is a, the array, which decays into a pointer + j, the offset.

a pointer + an offset results in a new pointer to the object at position j in the array.

Link to post
Share on other sites

16 minutes ago, GR412 said:

I know that part I'm just unsure how I should layout the parameters in the swap function declaration.

declare a buffer integer, set it to be equal to a[j], then make a[j] equal to a[j+1] make a[j+1] equal to the buffer.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

19 minutes ago, Sauron said:

declare a buffer integer, set it to be equal to a[j], then make a[j] equal to a[j+1] make a[j+1] equal to the buffer.

Can you show me what you mean because i'm under the impression you've still giving me the swap function body code, not the declaration parameters.

   
   
Link to post
Share on other sites

31 minutes ago, Unimportant said:

Generally, one provides pointers to the actual elements to swap to a swap function so you don't have to pass along the array itself.

 


void SWAP(int *a, int *b)
{
	int Temp = *a;	
  	*a = *b;
  	*b = Temp;
}

Then you can call it as such:


SWAP(a + j, a + j + 1);

Wich is a, the array, which decays into a pointer + j, the offset.

a pointer + an offset results in a new pointer to the object at position j in the array.

I'm trying to do it with the code i've been given, ther swap call for it is: SWAP(a,j,j+1); I need to somhow implement that call into a swap function.

   
   
Link to post
Share on other sites

void swap(auto* pBuffer, int i, int j)
{
    auto tmp = pBuffer;
    pBuffer = pBuffer[j];
    pBuffer[j] = tmp;
}

 

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

33 minutes ago, GR412 said:

Can you show me what you mean because i'm under the impression you've still giving me the swap function body code, not the declaration parameters.

Oh wait, you mean whether you need a pointer or something else? Arrays can be passed as a copy of the array (which is what you DON'T want) or as pointers. You can find more precise information and examples here https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm

 

j and j+1 are just integers and there's no harm in passing them as copies of the original, so you can write that the second and third parameters are just ints.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

void swap(int *v, int i, int j)
{
	int tmp=v[i];
	v[i]=v[j];
	v[j]=tmp;
}

This will swap the elements at the i and j index of an array v, which seems to be what you want to do.

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 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

×