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
M

Member_15233409

@Member_15233409
About
Posts
1
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • concern to shaker sort arlgorithm
    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

    Algorithms algorithms data-structures
  • Login

  • Don't have an account? Register

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