logo
down
shadow

List sorting with bubble sort (list of list)


List sorting with bubble sort (list of list)

By : Acapella Love
Date : November 21 2020, 07:35 AM
like below fixes the issue Problem is here Countries[j+1][2] = Countries[j][2] you have to replace with Countries[j+1] = Countries[j]
Also
code :
if not madeSwap:
                return
def bubblesort():
        Countries = [["Afghanistan",647500.0,3],["Albania",28748.0,1],["Zimbabwe",390580.0,2]]
        for i in range(0,len(Countries)):
            madeSwap = False
            for j in range (0,len(Countries)-(i+1)):
                if Countries[j][2] > Countries[j+1][2]:
                    temp = Countries[j+1]
                    Countries[j+1] = Countries[j]
                    Countries[j] = temp
                    madeSwap = True
            if not madeSwap:
                return Countries
        return Countries

>>> bubblesort()
[['Afghanistan', 647500.0, 1], ['Zimbabwe', 390580.0, 2], ['Albania', 28748.0, 3]]


Share : facebook icon twitter icon
Why when I reverse the comparison sign in my bubble sort algorithm does the resulting sorting list not reverse?

Why when I reverse the comparison sign in my bubble sort algorithm does the resulting sorting list not reverse?


By : scalaman
Date : March 29 2020, 07:55 AM
seems to work fine Java Code: , rolls sleeves up
You're inverting the sign on the wrong line.
Sorting a List using bubble sort

Sorting a List using bubble sort


By : alexubinik
Date : March 29 2020, 07:55 AM
I wish this help you , use Sort.bubble(list); this works fine i run in my PC
How do I sort an STL list object using list::sort() while passing my custom sorting function as a parameter to list::sor

How do I sort an STL list object using list::sort() while passing my custom sorting function as a parameter to list::sor


By : Alessio Gavioli
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I believe that it needs to be a free function, or declare it static static bool comparePriority( const ProcessControlBlock&, const ProcessControlBlock&);. The alternative is to wrap it inside a struct so it could be used as a functor (as described in the link posted in the comment.)
Because your code has numerous other compile errors, I'm finding it difficult to write up an example for you.
code :
bool comparePriority( const ProcessControlBlock& first,  
                              const ProcessControlBlock& second)
{
    bool isLessThan = false;

    if(first.getJobPriority() < second.getJobPriority())
    {
        isLessThan = true;
    }

    return isLessThan;
}

void JobTable::sortJobTableByPriority()
{
    jobTable.sort(comparePriority);
}
Trying to bubble sort a linked list by only rearranging pointers. My code has a workaround for a 2 node list that I want

Trying to bubble sort a linked list by only rearranging pointers. My code has a workaround for a 2 node list that I want


By : Shashank P
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can play a classic trick: take a pointer head and make it subsequently point to the next member of the previous node:
code :
Node *sortList(Node *head) {
    // make sure the list is not empty 
    if (head != NULL) {
        // bubble sort
        for (;;) {
            int swapped = 0;
            Node **linkp = &head;
            Node *currNode = head;
            Node *nextNode;

            while ((nextNode = currNode->next) != NULL) {
                if (strcmp(currNode->word, nextNode->word) > 0) {
                    currNode->next = nextNode->next;
                    nextNode->next = currNode;
                    *linkp = currNode = nextNode;
                    swapped++;
                }
                link = &currNode->next;
                currNode = nextNode;
            }
            if (swapped == 0)
                break;
        }
    }
    return head;
}
How to fix typeerror: 'list' object not callable error in bubble sorting a list while working in Python

How to fix typeerror: 'list' object not callable error in bubble sorting a list while working in Python


By : Steven-Johns
Date : March 29 2020, 07:55 AM
will help you list(j+1) is wrong. 'list' object is not callable. Also in bubble sort, the second loop is till second last element in the list ie. range(len(list)-1)
code :
def swap(a,b):
    if a > b:
        t = a
        a = b
        b = t
        return a,b
def sort(list):
    k=0
    for i in range(len(list)):
        for j in range(len(list)-1): #Change Here
            if list[j] > list[j+1]:
                (a,b) = swap(list[j],list[j+1]) #Change here
                list[j] = a
                list[j+1] = b
                k+=1
                print(k,'  ',list)
            else:
                k+=1
                continue

    print(list)

list = [5,6,89,2,1,42,23,46,0,83]
sort(list)
3    [5, 6, 2, 89, 1, 42, 23, 46, 0, 83]
4    [5, 6, 2, 1, 89, 42, 23, 46, 0, 83]
5    [5, 6, 2, 1, 42, 89, 23, 46, 0, 83]
6    [5, 6, 2, 1, 42, 23, 89, 46, 0, 83]
7    [5, 6, 2, 1, 42, 23, 46, 89, 0, 83]
8    [5, 6, 2, 1, 42, 23, 46, 0, 89, 83]
9    [5, 6, 2, 1, 42, 23, 46, 0, 83, 89]
11    [5, 2, 6, 1, 42, 23, 46, 0, 83, 89]
12    [5, 2, 1, 6, 42, 23, 46, 0, 83, 89]
14    [5, 2, 1, 6, 23, 42, 46, 0, 83, 89]
16    [5, 2, 1, 6, 23, 42, 0, 46, 83, 89]
19    [2, 5, 1, 6, 23, 42, 0, 46, 83, 89]
20    [2, 1, 5, 6, 23, 42, 0, 46, 83, 89]
24    [2, 1, 5, 6, 23, 0, 42, 46, 83, 89]
28    [1, 2, 5, 6, 23, 0, 42, 46, 83, 89]
32    [1, 2, 5, 6, 0, 23, 42, 46, 83, 89]
40    [1, 2, 5, 0, 6, 23, 42, 46, 83, 89]
48    [1, 2, 0, 5, 6, 23, 42, 46, 83, 89]
56    [1, 0, 2, 5, 6, 23, 42, 46, 83, 89]
64    [0, 1, 2, 5, 6, 23, 42, 46, 83, 89]
[0, 1, 2, 5, 6, 23, 42, 46, 83, 89]
Related Posts Related Posts :
  • Filtering from data
  • Where is the problem about selenium with python?
  • ansible custom filter fails when importing python library
  • How to assign the label of one column to the new one based on group maximum in pandas
  • What is the best approach for isolating a single area of similar colour?
  • Creating multiple clients for topics
  • Why is my 'for loop', despite iterating over all keys, only acting on the last one?
  • Can someone tell me what's wrong, when I run it the browsers says "This site can’t be reached"
  • Error in setting up mitmproxy on alpine 3.9
  • From traditional loop to list comprehension
  • Django celery unregistered task | relative imports
  • How to add elements in a multi dimensional array
  • Async await with sqs receive messages not working properly
  • What is definition of 'NAME' in Python grammar
  • Easy method to move rows from df to another with coditions?
  • Changing the size of only a single plot in matplotlib, without altering figure parameters
  • Fastest way to use Vision API on 10,000+ images with python
  • How to install nvidia apex on Google Colab
  • Random numbers Continuous in python
  • Fetching data after a certain time interval(10 sec) from a continuously increasing database like mysql using flask
  • Using VLOOKUP with merge in Python
  • Calculate geographical distance between 5 cities with all the possible combinations of each city
  • How to filter a pandas dataframe using multiple partial strings?
  • Pygame- make bullet shoot toward cursor direction
  • Create SEQUENCE based dictionary from list
  • How to fix broken link from Django MEDIA_ROOT?
  • How can I display the current time left in a timer in a label?
  • Compute number of occurance of each value and Sum another column in Pandas
  • How to separate the prefix in words that are 'di'?
  • Handling network errors from an external API across an application
  • Want a pandas Series of Trips Completed to count(Request) ratio for each hour as index for the given dataframe
  • Access dict keys and list elements by same index to loop over and assign values
  • Find rows from the same dataframe based on condition
  • Read only specific part first two lines from text file in python
  • Python How to convert string to dataframe?
  • How to fix this my error code program? I use Python 3.6
  • Is there a way of getting this string down to 3 words?
  • Large difference between overall F Score for a custom Spacy NER model and Individual Entity F Score
  • Drop rows where timestamps are older than subsequent row
  • Implement a bottle spin
  • Unable to convert widows epoch time to normal date time
  • Values from a XML file
  • PyAudio readframes not ending when wav file completes
  • Could not load the module
  • How to change datetime.datetime(2012, 1, 1, 0, 0) to 1/1/2012 in Python?
  • How to create ASN.1 Sequence without NamedType?
  • How to locate specific sequences of words in a sentence efficiently
  • How can I generate a multi-step process in Django without changing pages (w/out a new request)?
  • Why does this list comprehension only "sometimes" work?
  • send html report with row collapsed
  • How to define a type hint to a argument (the argument's value is a class, all expected value is a subclass of a certain
  • How do I send a styled pandas DataFrame by e-mail without losing the format?
  • How to view/average a groupby dataframe when the data is a string?
  • Django 2.2 staticfiles do not work in development
  • Flag to enable/disable numba JIT compilation?
  • Trying to split byte in a byte array into two nibbles
  • Error in Query - missing FROM-clause entry for table - SQL
  • Reading double c structures of Dll with Ctypes in Python
  • Autofill missing row in database based on missing time range
  • Get the max of a nested dictionary
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org