| |

VerySource

 Forgot password?
 Register
Search
View: 944|Reply: 6

How to implement bubble sorting with pointers

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-13 14:00:01
| Show all posts |Read mode
In C language, using pointers to implement bubble sort, how should I write it. Thank you for waiting online
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-18 15:45:01
| Show all posts
#include <stdio.h>
main ()
{
 int n, i, j, * temp, * p, num;
 printf ("input sum of the numbers\n");
 scanf ("% d",&n);
 num = n;
 printf ("input numbers n <% d", num);
 printf ("\n");
 for (i = 0; i <n; i ++) scanf ("% d", p + i);
 getchar ();
 for (i = 0; i <n; i ++)
  for (j = 0; j <n-i; j ++)
   if (* (p + j + 1) <* (p + j))
    (* temp = * (p + j); * (p + j) = * (p + j + 1); * (p + j + 1) = * temp;) / * bubble exchange * /
 printf ("the sort:\n");
 for (i = 0; i <n; i ++) printf ("% d", * (p + i));
 printf ("\n");
 getchar ();
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-18 16:00:01
| Show all posts
Never adjusted
I hope everyone corrects it!
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-20 15:09:01
| Show all posts
FT. Basic Skills
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 15:27:01
| Show all posts
typedef struct node
{
  int data;
} Node;
typedef int (* compare) (void *, void *); // This is a comparison function, just like the comparison function in the library sort function
void sort (Node * h [], int n, compare fun) // h [] puts an array pointing to Node
{
  int i, j;
  Node * tmp;

  for (i = 1; i <n; i ++)
  {
       for (j = i; j <n; j ++)
       {
            if (fun (h [j-1], h [j]))
            {
                  tmp = h [j-1];
                  h [j-1] = h [j];
                  h [j] = tmp;
             }
       }
  }
}
Wrote it myself
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-1 17:18:01
| Show all posts
Ok..
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-11 15:00:01
| Show all posts
I don't have tools in my hand, but it seems to be a problem!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list