0% found this document useful (0 votes)
50 views2 pages

Assignment 7

1. The document provides instructions for an assignment to implement an inline counting sort algorithm. Students are asked to sort an integer array A in-place without using an output array, by modifying the count array C to directly update the values in A. 2. The algorithm should work for integers in the range -k to k, read the size n and limit k from the user, generate a random input array A, sort A inline using counting sort, and print A before and after sorting. 3. The running time should remain O(n+k).

Uploaded by

piyank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

Assignment 7

1. The document provides instructions for an assignment to implement an inline counting sort algorithm. Students are asked to sort an integer array A in-place without using an output array, by modifying the count array C to directly update the values in A. 2. The algorithm should work for integers in the range -k to k, read the size n and limit k from the user, generate a random input array A, sort A inline using counting sort, and print A before and after sorting. 3. The running time should remain O(n+k).

Uploaded by

piyank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS21003/CS29003

http://cse.iitkgp.ac.in/~abhij/course/theory/Algo1...

CS29003 Algorithms
Laboratory

Autumn 2013, L-T-P: 0-0-3


Assignment No 7
Inline Counting Sort

Implement the counting sort algorithm taught in the class with two variations:
1. The entries in the input array A are now allowed to be integer values in the
range k to +k.
2. You are not allowed to use the output array B. Use the count array C, and in
the final pass through the array A, modify A itself so that A is sorted inline.
This version of counting sort need not be stable.
Notice that once you prepare the count array, you may think about printing the
integers k, k + 1, k + 2, ..., k 1, k in that sequence the required numbers of
times. In practice, however, you sort a record of items with respect to some key
value. Therefore, this flat listing of only the key values is not treated as sorting A.
Use suitable swap operations to effect inline sorting. The running time of the inline
sorting should continue to remain O(n + k).
Write a program to do the following.
Write a function that takes as input the array A, its size n, and the limit . The
function sorts A according to the inline counting sort algorithm.
Write a main() function that reads n and k from the user. It then creates a
random array A with n integer entries each in the range k to +k. It calls the
above function to sort A. The array A is printed before and after sorting.

Sample Output
n = 100
k = 10
+++ The
4
1
0
-5
-5

1 of 2

array before sorting


0
-1
6
-3
3
-9
1
2
9 -10
10
-2
1
2 -10
-4
9
5
3
-6
9
-5
2
-3
4

-3
4
4
-9
5

6
-10
5
0
2

7
1
4
-3
8

8
-1
-9
-9
-1

4
6
-4
6
2

10
4
-7
10
-3

-5
-3
7
1
2

2
-1
-6
-4
10

0
-8
5
-9
2

-5
1
1
-9
-1

Monday 24 November 2014 03:20 AM

CS21003/CS29003

http://cse.iitkgp.ac.in/~abhij/course/theory/Algo1...
7
5

-7
4

10
3

-8
-6

-3

-1

-2

-8

-5

-8

-1

+++ The array after sorting


-10 -10 -10
-9
-9
-9
-6
-6
-5
-5
-5
-5
-3
-3
-2
-2
-1
-1
1
1
1
1
1
1
3
3
3
4
4
4
6
6
6
6
6
7
10
10
10
10

-9
-5
-1
1
4
7

-9
-5
-1
2
4
7

-9
-4
-1
2
4
8

-8
-4
-1
2
4
8

-8
-4
-1
2
4
9

-8
-3
0
2
5
9

-8
-3
0
2
5
9

-7
-3
0
2
5
9

-7
-3
0
2
5
9

-6
-3
0
3
5
10

Submission site | Miscellaneous information | Home

2 of 2

Monday 24 November 2014 03:20 AM

You might also like