| |

VerySource

 Forgot password?
 Register
Search
View: 1817|Reply: 13

How to solve sorting problems in C ++? ? ? ?

[Copy link]

3

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 14:40:01
| Show all posts |Read mode
There are integer sequences {5,6,3,8,3,9,7,2,1,4,0}, the programming is sorted from big to small, and the sorted information is printed

The result is: 9 8 7 6 5 4 3 2 1 0

Thank you ~~~~
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-6 16:45:01
| Show all posts
See QSort function in C
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-9 17:18:01
| Show all posts
Bubbles, the easiest
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-9 21:45:01
| Show all posts
#include <functional>
#include <algorithm>
using namespace std;
int main (int argc, char * argv [])
{
int sz [] = {5,6,3,8,3,9,7,2,1,4,0};
sort (sz, sz + sizeof (sz) / sizeof (sz [0]), greater <int> ());
return 0;
}
Reply

Use magic Report

0

Threads

9

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-4 11:30:02
| Show all posts
Check out the books on "Data Structures" and "Algorithms".
Reply

Use magic Report

2

Threads

20

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-7-5 13:45:01
| Show all posts
If you do not consider the execution efficiency, bubbling or simple selection sorting is the easiest
If you consider efficiency, you must use quick sort or Hill sort, heap sort, etc.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-12 19:45:01
| Show all posts
You can find it just by looking for it, there will definitely be in the book of data structure.
It is not difficult to write one yourself.

#include <iostream>
using namespace std;

#define SUM 10

template <class T>
void Sort(T a[], int N)
{
for(int i=0; i<N-1; i++){
for(int j=i+1; j<N; j++){
if(a[i]<a[j]) swap(a[i], a[j]);
}
}

}
int main()
{
int a[]={5,6,8,3,9,7,2,1,4,0};
Sort(a, SUM);
for(int i=0; i<10; i++)
cout << a[i] << "";

return 0;
}
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-7-12 20:45:01
| Show all posts
sort() generic algorithm
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-16 09:30:02
| Show all posts
void sort(int *a,int n)
{
    a[10]={{5,6,3,8,3,9,7,2,1,4,0};
    for(int i=1;i<10;i++)
    {
       int k=i-1;
       for(j=k;j<10;j++) if(a[k]<a[j]) k=j;
       int temp=a[i];a[i]=a[k];a[k]=temp;
    }
}
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-16 11:00:01
| Show all posts
int swap(int A[], int n)
{
   int i, j, temp;
   for(i = 0; i <n; i++)
      for(j = i + 1; j <n-1; j ++)
          if(A[i]<A[j])
               {
                 temp = A[j];
                 A[i] = A[j];
                 A[j] = temp;
                }
  return 1;
}

main ()
{
   int A[10];
   swap(A, 10);
   for(int i = 0 ;i <10; i++)
     printf("%d, ", A[i]);
}
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