Question

So here is my problem: I am trying to do a form of insertion sort. Actually, I am trying to search through an ArrayList using a Binary Search algorithm and find where to insert a string. What I have so far sort of works. It is in partial order. I have been stumped on this for over a week! Below is my code:

EDIT: Sorry I think I confused people. My question is how can I edit this to work properly. It inserts my objects in partial order. I need it to be complete order! I do not know where this is happening. I have far too much data being parsed to debug this line for line too.

    private void insertOrdered(int frontParameter, int endParameter, TwitterData user) {
    int front = frontParameter;
    int end = endParameter;
    int mid = (front+end)/2;

    if (front > end) {
        if (user.getUsername().equalsIgnoreCase(users.get(mid).getUsername()))
            users.get(mid).addTweet(user.getTweets().get(0));
        else
            users.add(mid, user);
    }

    if (user.getUsername().toLowerCase().compareTo(users.get(mid).getUsername().toLowerCase()) < 0) {
        insertOrdered(front, mid - 1, user);
    }

    else if (user.getUsername().toLowerCase().compareTo(users.get(mid).getUsername().toLowerCase()) > 0) {
        insertOrdered(mid + 1, end, user);
    }

    else {    //will get to this case if the usernames being tested are equal
        users.get(mid).addTweet(user.getTweets().get(0)); //if the user is already in the list, just add the tweet. It is assumed that the user being passed in will only have one tweet tied to their information hence the get(0)
    }
}

Just for some background information, I am using this for an ArrayList of Twitter usernames and their associated tweets. The parameter passed, user, is a TwitterData object of a class I wrote. For all intensive purposes all you need to know if I can retrieve the username and a list of tweets that user may have tweeted. Below is a test output of the first 100 users of the list to show you what I mean by it partially working.

First 100 users output:

4colorrebellion
50BandBuckie
2996mymy
20120040
_littlethugg
_IndyaWithaWHY_
__PrettyMistake
__Mannyy24
_MikeFishh
_NikeDeshaun_
_TinaBeana
_PrincesaNessa
_LoveInPaaaris
_Victoria_Ortiz
adriannstacey21
ahansen718
action_packed_
Alicemegan93
alexgracehunter
AlishaaShaffer
arowsey_15
Amy_Vee
allycolucci
AmbiTious___xO
aguss__A
averybrownn
babbyyy_itsREAL
ando775
bburns1117
amberdorais
AshMarieMonica
Ashton_45
_SarahJustine
BlasianCocaine
belieber_pride
AyeeIts_DeeDee
BrianHodges
BritFranceNews
Big_Red911
BiteMy_SlimJim
BadGirlYon
Cemonee_Allisse
cathy_riveros
byby_35
CEOSIXX
busybeekatie
ChelsiKatherine
BOOBtifulJohnny
Coolie_Mackin
coralreefer420
CrashBandaCooch
codyalexander23
cubanrice
corrinea143
Cyndi_R82
danny728_
dbangin
ASNievera
DeAndre_Johnson
Deion_Hungry
DStudmuffin
cowellEmma
expired_data
Dr_drew_V93
feather_hinn
DominiqueQ2
getbackamistake
Da_Dirty_Dern
dudeimisaac
elennatalbert
evillurking
fANNcy_
covolm4
HimOverHere
DameLush
erinnnroach
freaky_fahfah
freesugardaddy
elhotpocket
FollowMandy
HaileyySorenson
DomoNinjaSwagg
IamSalinaScott
fredthemarauder
IAmTHATguy_1
facucuellar
iDream_Mindless
hirschy_kiss94
freshmoney5
HannahMcC_x
GarrieBrocato
AyeeeCali
iSexTattedDudes
Illumi_Lani
itsyunk
jahzzi
Jamie_Hill101
iHeartAudiooooX
jaymethornley
JasonMyers18

One more thing, the last else case does work properly. I have eliminated any kind of dual users being submitted to the ArrayList. Any ideas?

Was it helpful?

Solution

This would be a lot simpler if you just inserted elements anywhere in the list and called Collections.sort() it would take the same amount of work as you already calculated for your insertion O(n*logn)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top