Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help implementing Insertion Sort on Subarray in Java
New Posts  All Forums:Forum Nav:

Help implementing Insertion Sort on Subarray in Java

post #1 of 2
Thread Starter 
I need to implement a method:

insertSort(int low, int high)

which can sort the subarray of an inputted array arr (from low to high, inclusive).

so if I had an array:
int[] arr = [3,7,6,5,10,4,2] and called arr.insertSort[2,5] it would change the array arr to be:
arr = [3,7,4,5,6,10,2], so it would sort elements 2 through 5 in the inputted array.

Here is my start, but I am just having trouble on when I should be swapping the elements positions.

private static void insertSort(int low, int high)
Macbook Air
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E6600 2.4ghz @ 3.6ghz Gigabyte EP45-UD3P Rev 1.0 BFG GTX275 4x1gb Corsair XMS2 DDR2-800 
Hard DriveOptical DriveOSMonitor
150gb Raptor, 3x1tb Storage Samsung 22x DVDRW + LG 22x DVDRW (BOTH SATA) Windows 7 64-Bit Samsung 245BW 24" 1920x1200 5ms 
KeyboardPowerCaseMouse
Logitech G11 Silverstone Strider 750w Modular Antec 900 Razer Copperhead 
Mouse Pad
Custom Outer Space 
  hide details  
Reply
Macbook Air
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E6600 2.4ghz @ 3.6ghz Gigabyte EP45-UD3P Rev 1.0 BFG GTX275 4x1gb Corsair XMS2 DDR2-800 
Hard DriveOptical DriveOSMonitor
150gb Raptor, 3x1tb Storage Samsung 22x DVDRW + LG 22x DVDRW (BOTH SATA) Windows 7 64-Bit Samsung 245BW 24" 1920x1200 5ms 
KeyboardPowerCaseMouse
Logitech G11 Silverstone Strider 750w Modular Antec 900 Razer Copperhead 
Mouse Pad
Custom Outer Space 
  hide details  
Reply
post #2 of 2
Here is my version in c++, low and high in your version is bottom and top in mine...pretty easy fix.
Code:
int arr [] = {3,7,6,5,10,4,2};

int bottom = 2;
int top = 5;

        int j;                     // the number of items sorted so far
        int key;                // the item to be inserted
        int i;  

for (j = bottom + 1; j <= top; j++)  // Start with bottom + 1
{
           key = arr[ j ];
           int p = j;

           // greater values will "slide" to the right
           for(i = j - 1; i >= bottom; --i) 
           {
                    if(arr[i] > key)
                        arr[ p ] = arr[ i ];
                    else if(arr[i] < key){
                          arr[i + 1] = key; // Places key in right position
                           break;
                    }
                    --p;
                   if(i == bottom)
                       arr[i] = key; 
           }
}


Edited by surfbumb - 10/21/13 at 1:48pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help implementing Insertion Sort on Subarray in Java