Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Sorting names by using bubble and insertion sort

martian94

Coder
Task:
Create a text file with at least 10 first names, line separated. This file should be unsorted. Make a program that
  • reads the names into a 1-dimensional table which is read line by line
  • the names in the list is to be sorted in ascending order (A-Z), you are not allowed to use the sort-function
  • the sorted list should be written to a new file, name by name from the list

You should use one version that use the sorting algorithm bubble sort without stop marker and one version that use insertion sorting.


I am a little stuck, how to do this with letters? Numbers are ok as from my former posts, but how to do this? All I have this far gives me the names as they are written in the .txt-file (unsorted) and with "None" between each name.

List of names if useful:
Code:
Anne
Jonas
Aase
David
Stine
Georg
Line
Brede
Camilla
Karl

My code this far:
Python:
name_list = {}

name_file = open('FirstNames.txt','r',encoding='utf-8')

first_name = name_file.readline().rstrip('\n')

while first_name !='':
    name_list[first_name] = None

    # Read next line
    first_name = name_file.readline().rstrip('\n')

name_file.close()

# Print the whole dictionary
print(name_list)
 
Why are you creating a dictionary ? Apart from overkill (you do not need key:value pairs) this is bad because if a name occurs more than once in your input only one gets stored. The assignment does not say to remove duplicates, or that the names must be unique.
Think about creating an array of strings. You can compare two strings in the same way as two numbers.
 
Why are you creating a dictionary ? Apart from overkill (you do not need key:value pairs) this is bad because if a name occurs more than once in your input only one gets stored. The assignment does not say to remove duplicates, or that the names must be unique.
Think about creating an array of strings. You can compare two strings in the same way as two numbers.
I am using ChatGPT a bit and it is a bit of an enigma to use. I mean, it makes the code work but it's not up to the standard of our tutor, tutor does not like us using shortcuts such as break, exit, etc.

I don't know why I am using a dictionary. I did not really think about duplicates.

If I were to create an array of strings, would it be feasible to just alter the bubble sort program and insertion sort program?

Sorry for being, ehrm, a little slow.
 
Hm, dangerous game to do your assignments that way. Yes you might get stuff done but you will not actually learn something.
If your two sort programs now work correctly they will work for strings also. For fun, just put quotes around al your numbers. I think it will work just as well. Then you can also use arbitrary strings.
 

New Threads

Buy us a coffee!

Back
Top Bottom