Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

Help with some algoritms


coin-god
 Share

Recommended Posts

I have to write a function in JAVA that modifies an array by using the first element as a pivot and moves all the elements that are smaller to it's left and the ones bigger to it's right. I have to use crossed recursion. The pivot is compared to each of the other elements only once.

 

Example:

 

3 4 1 8 6 5 2

 

Should end up like this (I think):

 

1 2 3 4 8 6 5 (3 is pivot)

 

I guess the actual order of the smaller/bigger elements dosn't matter.

I honestly have no idea of were to start. They didn't give use much information about how to do this kind of stuff in college.

This is not "homework", I'm studing for my final exam.

Yl8KS.jpg
Link to comment
Share on other sites

Are you sure you are supposed to use recursion for pivoting? This is part of the quick sort algorithm, which indeed, is done recursively. You could make even the pivot recursive, but I don't see why you'd want to. There are several ways of doing this. The most typical actually used by quick sort would proceed like this.

 

 

Indecies of two pointers start at two ends of array, excluding pivot.3 4 1 8 6 5 2 One on the left is > pivot, while right < pivot. Swap.* ^         ^3 2 1 8 6 5 4 Left pointer is < pivot, so pointer is advanced. Right > pivot, advanced.* ^         ^3 2 1 8 6 5 4 Again, both are correct, so both are advanced.*   ^     ^3 2 1 8 6 5 4 Left pointer > pivot. Right > pivot. Left stays, right advances.*     ^ ^3 2 1 8 6 5 4 Pointers merged. Now we need to step back, and swap with pivot.*     ^3 2 1 8 6 5 4 Perform swap.^   ^1 2 3 8 6 5 4 Final state. To do full quick sort, repeat with 1 and 8 as pivots, etc.

 

 

Code for it would look something like this. (C syntax)

 

 

a=1;b=length-1;while(a!=b){  if(array[a]<array[0]){a++; continue;}  if(array[b]>array[0]){b--; continue;}  swap(array+a, array+b);}swap(array, array+a-1);

 

 

If you insist on this part being done recursively, you can make it run something along the lines of this.

 

 

void pivot(int array[], int a, int b){   if(a==b){swap(array, array+a-1);return;}   if(array[a]<array[0]){pivot(array,a+1,b); return;}   if(array[b]>array[0]){pivot(array,a,b-1); return;}   swap(array+a, array+b);   pivot(array,a,b);}pivot(array, 1, length-1);

 

 

But honestly, that's just a waste of stack space and jump instructions.

 

Full quick sort written to implement the first form with recursion would be written like this.

 

void quicksort(int array[], int length){   if(length<=1)return;   a=1;   b=length-1;   while(a!=b)   {     if(array[a]<array[0]){a++; continue;}     if(array[b]>array[0]){b--; continue;}     swap(array+a, array+b);   }   swap(array, array+a-1);   quicksort(array, a-1);   quicksort(array+a,length-a);}

 

Edited by K^2

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
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
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.