Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with insertion sort for an array of strings in java.
New Posts  All Forums:Forum Nav:

Help with insertion sort for an array of strings in java.

post #1 of 8
Thread Starter 
hello again, well Im currently finishing off my programming assignment, and I thought everything is working (fixing up the occasional spelling error/small bug) until I get to my sorter for my media list, turns out that its utterly broken frown.gif

so all I really need is someone to show me how to do an insertion sort using an array of string values from what is below.
int ListLength = //a known length;
String Unsorted = new String[ListLength];
int count = 0;

BufferedReader UnsortRead = new BufferedReader(new FileReader("c:\\Borrowing\\Media List.txt"));
While((Unsorted[count] = readLine()wink.gif != null)
{
count++
}

and then here the insertion sorter would go and it would sort the array full of strings. finally it then has to be saved to the same file.

thanks for anyone who can help.
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #2 of 8
And you're not allowed to use just Arrays.sort(leArray)?
Most forums I think you're not allowed to write people's homework for them. Take a look at the pseudocode on wiki's page.
http://en.wikipedia.org/wiki/Insertion_sort
Writing the java equivalent should be pretty simple, just substitute java syntax in and refine from there.
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
post #3 of 8
Thread Starter 
Quote:
Originally Posted by S0ULphIRE;14278520 
And you're not allowed to use just Arrays.sort(leArray)?
Most forums I think you're not allowed to write people's homework for them. Take a look at the pseudocode on wiki's page.
http://en.wikipedia.org/wiki/Insertion_sort
Writing the java equivalent should be pretty simple, just substitute java syntax in and refine from there.

I'm not asking you do to do my homework, this is a 2400 line assignment at its current length, this is a simple part that is one of the many features of the program, its just the for some reason I keep getting a null pointer exception, I'll copy the code over.

int SortedLastPosition = 0;

for(int i = 0; i
{
Hold = UnSorted;
if(Sorted[0] == null)
{
Sorted[0]= Hold;
}
else
{
if(UnSorted
.compareTo(Sorted[SortedLastPosition])>=0)
{
SortedLastPosition++;
Sorted[SortedLastPosition] = Hold;
}
else
{
MoveBack = SortedLastPosition;
while((UnSorted.compareTo(Sorted[MoveBack])<0) && (MoveBack+1 != NewTally) && LoopBreaker == 0)
{
Sorted[MoveBack+1] = Sorted[MoveBack];
Sorted[MoveBack] = null;
if((Sorted[MoveBack-1] != null) && (MoveBack-1 == 0))
{
MoveBack--;
}
else
{
LoopBreaker = 1;
}
}
SortedLastPosition++;
Sorted[MoveBack] = Hold;
}
}
}
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #4 of 8
Lol ok, I'll take a look soon, at work atm. Assuming you want strings sorted alphabetically via first character (is that a pleonastic statement? tongue.gif I like that word).
Edited by S0ULphIRE - 7/20/11 at 3:19am
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
post #5 of 8
Thread Starter 
yes that is correct : ) and interesting, I just learnt a new word
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #6 of 8
Thread Starter 
OK well I ended up fixing it, took only 3 hours *facepalm*
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by donthaveacow;14279481 
OK well I ended up fixing it, took only 3 hours *facepalm*

Glad to hear you figured it out. smile.gif

Next time, for clarity, use the code tags when you're pasting source code. Also, the actual compiler error with line number helps, too smile.gif.
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #8 of 8
I wrote it last night anyway, compare it to yours!
Code:
public static void main(String[] args) {
       // int ListLength = 5;//a known length;
        String[] Unsorted = new String[]{"Z","B","A","F","D"};
        String[] Sorted = new String[Unsorted.length];
        int count = 0;
        
        try{
            BufferedReader UnsortRead = new BufferedReader(new FileReader("c:\\Borrowing\\Media List.txt"));
            while((Unsorted[count] = UnsortRead.readLine()) != null)
            {
                count++;
            }
            } catch(Exception e){
                //yeeheehee;
        }
        Sorted = sort_sub(Unsorted, Unsorted.length); // calls sort subroutine
    }

//Just call this when you want to sort a string
    public static String[] sort_sub(String array[], int f){
        String temp="";
        for(int i=0;i<f;i++){
            for(int j=i+1;j<f;j++){
               if(array[i].compareToIgnoreCase(array[j])>0){
                   temp = array[i];
                   array[i]=array[j];
                   array[j]=temp;
               }
            }
        }
        return array;
    }

Edited by S0ULphIRE - 7/20/11 at 7:28pm
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  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 with insertion sort for an array of strings in java.