Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Algorithms
  4. concern to shaker sort arlgorithm

concern to shaker sort arlgorithm

Scheduled Pinned Locked Moved Algorithms
algorithmsdata-structures
2 Posts 2 Posters 25 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15233409
    wrote on last edited by
    #1

    I am currently taking a exercise is the shaker sort algorithm.Instead of separating two for loops, I have included a for-j loop inside a for-i loop. Please check my code.

    public static void shakerSort(int array[], int n)
    {
    boolean swapped = true;
    while (swapped)
    {
    swapped = false;
    for (int i = 0; i < n - 1; i++)
    {
    if (array[i] > array[i + 1])
    {
    int temp = array[i];
    array[i] = array[i + 1];
    array[i + 1] = temp;
    swapped = true;
    }

             if (i == n - 2) 
             {
                 if (!swappped)
                 {
                    break;
                 }
                 else
                 {
                     swapped = false;
                     for (int j = n - 1; j > 0; j--) 
                     {
                          if (array\[j - 1\] > array\[j\]) 
                          {
                               int temp = array\[j\];
                               array\[j\] = array\[j - 1\];
                               array\[j - 1\] = temp;
                               swapped = true;
                          }
                     }
                     if (!swapped) 
                     {
                       break;
                     }
                 }     
             }
        }
    }
    

    Output:

    -5
    -3
    5
    9
    11
    12
    15
    18

    I hope you can rate my code .Thank you very much

    Richard Andrew x64R 1 Reply Last reply
    0
    • M Member_15233409

      I am currently taking a exercise is the shaker sort algorithm.Instead of separating two for loops, I have included a for-j loop inside a for-i loop. Please check my code.

      public static void shakerSort(int array[], int n)
      {
      boolean swapped = true;
      while (swapped)
      {
      swapped = false;
      for (int i = 0; i < n - 1; i++)
      {
      if (array[i] > array[i + 1])
      {
      int temp = array[i];
      array[i] = array[i + 1];
      array[i + 1] = temp;
      swapped = true;
      }

               if (i == n - 2) 
               {
                   if (!swappped)
                   {
                      break;
                   }
                   else
                   {
                       swapped = false;
                       for (int j = n - 1; j > 0; j--) 
                       {
                            if (array\[j - 1\] > array\[j\]) 
                            {
                                 int temp = array\[j\];
                                 array\[j\] = array\[j - 1\];
                                 array\[j - 1\] = temp;
                                 swapped = true;
                            }
                       }
                       if (!swapped) 
                       {
                         break;
                       }
                   }     
               }
          }
      }
      

      Output:

      -5
      -3
      5
      9
      11
      12
      15
      18

      I hope you can rate my code .Thank you very much

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Member 15233409 wrote:

      I hope you can rate my code .

      I give your code a 4.5 rating. You're welcome.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups