Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python, i have a .txt file that has rows of numbers. For example: I need to import this file above .txt file and sort

In python, i have a .txt file that has rows of numbers. For example:

image text in transcribed

I need to import this file above .txt file and sort the integers in each row. The problem I now have is that the integers in the rows arent being sorted and I need them to be, just the rows themselves are being sorted right now as shown below.

image text in transcribed

This is the code I have from the sort (geeksforgeeks):

image text in transcribed

image text in transcribed

Also if possible, I would like to know how to remove the first number from each row and only sort everything from position [1] and larger only. So 4 19 2 5 11 would only sort 19 2 5 11.

1 2 3 4 14 192511 812345612 50 -537-2 1234 New Line 50 -5 3 7 -2 New Line 1 2 3 4 This is array : [['4', '19', '2', '5', '11'], ['8', '1', '2', '3', '4', '5', '6', '1', '2'], ['5', '0', '-5', '3', '7', '-2'], ['1', '2', '3', '4'1] Given array is 4 19 2 5 11 8 1 2 3 4 5 6 1 2 50 -5 3 7 -2 1 2 3 4 Sorted array is: 1 2 3 4 4 19 2 5 11 5 0 -5 3 7 -2 8 1 2 3 4 5 6 1 2 def mergeSort(arr): for i in range(len(arr)): #loop for j in range(len(arr[i])): #loop arr[i][j]=int(arr[i][j]) #converting str to int if len(arr) > 1: # Finding the mid of the array mid = len(arr) // 2 # Dividing the array elements L = arr[:mid] # into 2 halves R = arr[mid:] # Sorting the first half mergeSort(L) # Sorting the second half mergeSort(R) i = j = k = 0 # Copy data to temp arrays L[] and R[] while i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions