| |

VerySource

 Forgot password?
 Register
Search
View: 1183|Reply: 5

Take a look at the master !!!!!!!!

[Copy link]

3

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-16 19:30:01
| Show all posts |Read mode
There is a one-dimensional integer array A of length n. An algorithm is designed to store all negative numbers in the array in the front of the array, and all positive numbers after the negative numbers.
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-6-25 13:15:01
| Show all posts
The landlord did not say what to do with 0, it is temporarily positive

void f(int a[], int n){
  int i=0,j;/*i find positive numbers, j find negative numbers*/
  while(i<n&&a[i]<0)i++;/*First positive number*/
  for(j=i+1;j<n;j++)
    if(a[j]<0){
      int temp = a[i];
      a[i] = a[j];
      a[j] = temp;
      i++; /* Both i and j are positive numbers, i is the first positive number */
    }
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 09:45:01
| Show all posts
The algorithm upstairs is slow
Quick sort is faster
A pointer from front to back
Another back to front
Can reduce poverty as much as possible
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 10:15:01
| Show all posts
Insert O(N) directly
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 10:30:02
| Show all posts
agree
Is indeed o(n)
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-18 12:30:01
| Show all posts
Count sorting, the count range is 2 (just distinguish between positive and negative values), O(n) complexity
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