Athlete Sort - HackerRank Solution in Python
Tags: python, algorithms, programming, beginners
Published: 2023-09-06 13:37:17

Athlete sort - HackerRank is medium difficulty problem of python based on sorting and nested list. It is part of python practice problems of HackerRank. In this post we will see how we can solve this challenge in Python

## Problem Statement and Explanation

In this problem statement we have to sort the given spreadsheet with respect to the `Kth` column. The spreadsheet consists of `N` rows and `M` columns. We have to print the sorted table.

Suppose the given spreadsheet is

| Sr. No. | Age | Marks | Height | Weight | Roll No. | class |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | 10 | 12 | 170 | 40 | 3 | 8 |
| 2 | 12 | 12 | 180 | 50 | 1 | 9 |
| 3 | 8 | 10 | 160 | 45 | 2 | 7 |

and we have to sort the spreadsheet with respect to the `Kth` column. Suppose `K = 1` then the sorted spreadsheet will be based on the `Age` column.

| Sr. No. | Age | Marks | Height | Weight | Roll No. | class |
| --- | --- | --- | --- | --- | --- | --- |
| 3 | 8 | 10 | 160 | 45 | 2 | 7 |
| 1 | 10 | 12 | 170 | 40 | 3 | 8 |
| 2 | 12 | 12 | 180 | 50 | 1 | 9 |

### Input Format

- The first line contains `N` and `M` separated by a space.
- The next `N` lines contains `M` elements separated by a space.
- In the last line we have to enter the value of `K`.

### Output Format

- Print the `N` lines of the sorted table. Each line should contain the space separated elements.

### Constraints

- `1 <= N, M <= 1000`
- `0 <= K < M`
- `X <= 100` where `X` is any element in the table.

**Note** : K is indexed from `0` to `M-1` i.e. the first column is indexed as `0`.

There are two solution provided in this post. First one is the using the inbuilt `sort()` and `lambda` function and the second one is using nested loops and comparing the values. If the first value is greater than the second value then we will swap the values.

After sorting the spreadsheet we have to print the sorted spreadsheet. The output of the above spreadsheet will be in console as

```
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12

```

## Athlete Sort HackerRank Solution in Python

{% gist https://gist.github.com/codeperfectplus/169bc72ddaeb4f70dfe01d33edfaadcb.js %}

## Time Complexity of the Solution

- The time complexity of the above first solution is `O(n log n)` because we are using the inbuilt `sort()` function which has the time complexity of `O(n log n)`.

- The time complexity of the above second solution is `O(n^2)` because we are using the nested loops and comparing the values. If the first value is greater than the second value then we will swap the values.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnbkbr8w9uigep6z16q5.png)

_Problem statement is taken from [Hackerrank](https://www.hackerrank.com/domains/python), and the solutions are implemented by **CodePerfectPlus** team_

## Other Article By Author

- [HackerRank Solve Python Algorithm Series](https://codeperfectplus.com/category/Solve-python)
- [HackerRank Problem Solving Algorithm Series](https://codeperfectplus.com/category/Problem-solving)
- [30 Days of Code HackerRank](https://codeperfectplus.com/category/Hackerank-30-days-of-code)
- [10 Days of JavaScript HackerRank](https://codeperfectplus.com/category/10-days-of-javascript)

### [30 Days of Code SubReddit](https://www.reddit.com/r/30daysofcode)