Question

In: Computer Science

data=[3831,4572,4219,2099,285,2391,3836,2290,410,4690,2031,1778,1468,4031,4513,1709,3603,4146,498,118,2635,3097,689,1340,617,394,3196,3676,4263,4110,2850,885,1926,2225,659,2643,4847,4579,977,2405,918,1256,4194,719,1218,3218,1969,395,156,3091,1860,44,2519,

data=[3831,4572,4219,2099,285,2391,3836,2290,410,4690,2031,1778,1468,4031,4513,1709,3603,4146,498,118,2635,3097,689,1340,617,394,3196,3676,4263,4110,2850,885,1926,2225,659,2643,4847,4579,977,2405,918,1256,4194,719,1218,3218,1969,395,156,3091,1860,44,2519,1763,2938,1246,1384,4034,2008,876,4346,1975,2612,2531,4518,4554,2187,3938,3396,434,4679,1681,2652,371,3884,1681,4636,3420,4880,111,566,2107,1810,4672,1716,303,1102,3777,3635,1485,2301,3127,2487,3058,1171,4240,4108,2990,334,3746,27,651,1299,1125,2475,2760,411,1932,4071,4099,4452,670,3229,2010,997,3274,2182,3072,42,4610,3657,844,1070,2697,322,4059,2255,3140,1162,1856,4111,4007,2599,4246,3508,2093,2597,1686,1014,158,1389,1032,1832,1481,2117,4947,4835,2820,2227,4469,1203,4665,3935,3946,792,1510,4940,3659,193,1603,3690,1531,761,2477,199,830,1920,1060,4717,3585,2830,3635,3205,3941,2677,3709,4544,3415,1222,3101,1010,4810,3052,3427,4702,2707,4488,458,1637,597,2105,1933,1587,231,1638,4713,1893,449,213,3822,1175,990,3349,440,2665,4248,2909,3221,1328,249,2576,4531,908,4302,728,3923,541,1330,4443,4655,2033,4761,4769,1540,48,600,2932,97,3161,2790,2093,2695,2735,3728,4879,2927,325,1244,1070,2056,2162,1367,4503,3924,3691,683,1553,716,4262,4760]

In PYTHON please write a code that implements the counting sort algorithm by making a CountingSort function with an array input. Please make the array input in the form of a list. Then sort 3 randomly generated arrays by writing code, and the data array listed below. Test all 4 arrays and print the output.

Please develop then comment on the growth function of the code, and comment on the big O notation.

Solutions

Expert Solution

def countingSort(array):
    size = len(array)
    output = [0] * size

    # Initialize count array
    maxNum = max(array) 
    count = [0] * (maxNum+1)

    # Store the count of each elements in count array
    for i in range(0, size):
        count[array[i]] += 1

    # Store the cummulative count
    for i in range(1, maxNum+1):
        count[i] += count[i - 1]

    # Find the index of each element of the original array in count array
    # place the elements in output array
    i = size - 1
    while i >= 0:
        output[count[array[i]] - 1] = array[i]
        count[array[i]] -= 1
        i -= 1

    # Copy the sorted elements into original array
    for i in range(0, size):
        array[i] = output[i]
import random
# To generate 3 random size for our Array
n1 = random.randint(1,15)
n2 = random.randint(1,15)
n3 = random.randint(1,15)
print(n1,n2,n3)

#Generating 3 random list using random size and random number's range

randomlist1 = []
for i in range(0,n1):
  n = random.randint(1,30)
  randomlist1.append(n)
print(randomlist1)
randomlist2 = []
for i in range(0,n2):
  n = random.randint(100,300)
  randomlist2.append(n)
print(randomlist2)
randomlist3 = []
for i in range(0,n3):
  n = random.randint(500,999)
  randomlist3.append(n)
print(randomlist3)

#Data Listen given by Useer
dataList = [3831,4572,4219,2099,285,2391,3836,2290,410,4690,2031,1778,1468,4031,4513,1709,3603,4146,498,118,2635,3097,689,1340,617,394,3196,3676,4263,4110,2850,885,1926,2225,659,2643,4847,4579,977,2405,918,1256,4194,719,1218,3218,1969,395,156,3091,1860,44,2519,1763,2938,1246,1384,4034,2008,876,4346,1975,2612,2531,4518,4554,2187,3938,3396,434,4679,1681,2652,371,3884,1681,4636,3420,4880,111,566,2107,1810,4672,1716,303,1102,3777,3635,1485,2301,3127,2487,3058,1171,4240,4108,2990,334,3746,27,651,1299,1125,2475,2760,411,1932,4071,4099,4452,670,3229,2010,997,3274,2182,3072,42,4610,3657,844,1070,2697,322,4059,2255,3140,1162,1856,4111,4007,2599,4246,3508,2093,2597,1686,1014,158,1389,1032,1832,1481,2117,4947,4835,2820,2227,4469,1203,4665,3935,3946,792,1510,4940,3659,193,1603,3690,1531,761,2477,199,830,1920,1060,4717,3585,2830,3635,3205,3941,2677,3709,4544,3415,1222,3101,1010,4810,3052,3427,4702,2707,4488,458,1637,597,2105,1933,1587,231,1638,4713,1893,449,213,3822,1175,990,3349,440,2665,4248,2909,3221,1328,249,2576,4531,908,4302,728,3923,541,1330,4443,4655,2033,4761,4769,1540,48,600,2932,97,3161,2790,2093,2695,2735,3728,4879,2927,325,1244,1070,2056,2162,1367,4503,3924,3691,683,1553,716,4262,4760]
countingSort(randomlist1)
print("Sorted Array in Ascending Order: ")
print(randomlist1)

countingSort(randomlist2)
print("Sorted Array in Ascending Order: ")
print(randomlist2)

countingSort(randomlist3)
print("Sorted Array in Ascending Order: ")
print(randomlist3)

countingSort(dataList)
print("Sorted Array in Ascending Order: ")
print(dataList)

There are mainly 4 for loops present within the function countSorting:

1st Loop - O(maxNum)

2nd Loop - O(size)

3rd Loop - O(maxNum)

4th Loop - O(size)

Overall Complexity - O(maxNum+size){ O(maxNum)+O(size)+O(maxNum)+O(size)}

For all the cases(Worst, Average, Best), the function is going to follow same number of iterations i.e complexity is not gonna differ in any of the case. It is dependent on arraysize and maximum element present within the array.

Hence O(n+k) where n is size of array and k is the maximum element.

But, it is bad if the integers are very large because the array of that size should be made.


Related Solutions

What is data saturation? When in the data collection process or data analysis process can data...
What is data saturation? When in the data collection process or data analysis process can data saturation be identified? How does a qualitative researcher know that he or she reached data saturation?
(TCO 8) Malicious data modification or tampering is an attack against data confidentiality. data integrity. data...
(TCO 8) Malicious data modification or tampering is an attack against data confidentiality. data integrity. data availability. data accountability. Question 105 pts (TCO 9) A threat assessment is a(n) identification of types of threats an organization might be exposed to. systematic rating of threats based upon level of risk and probability. potential level of impact. likelihood of a threat materializing. Question 115 pts (TCO 10) When it comes to HIPAA, which of the following does PHI stand for? Public health...
What is data? Does data have to be collected for it to be considered data? How...
What is data? Does data have to be collected for it to be considered data? How do data scientists use data?
What is a Data? What is a quantitative data? What are various other forms of Data?...
What is a Data? What is a quantitative data? What are various other forms of Data? *ON SHEETS, HANDWRITTEN * REFERENCES
7. The data model that addresses the data requirements is the _____. Logical data model Conceptual...
7. The data model that addresses the data requirements is the _____. Logical data model Conceptual data model Relational database Physical data model 8. The hierarchical data base relationships are called _____. Structured Parent and child Table Owners and members 9. Java is used in what type of database? Relational Object-oriented Multidimensional Hierarchical 10. Data repositories are _____. A proprietary database Used solely for clinical data A database used solely for video and audio An open-structure database
Description of Data The data being used for this lab are based on data collected after...
Description of Data The data being used for this lab are based on data collected after the 2009 war ended that was between Israel and the Hamas militias in the Gaza Strip. Responses were obtained from 200 individuals that were exposed to rocket attacks during that period. The variables in the study include: ID: Used to indicate the ID# of the participant family income: The household income of the individuals (expressed in thousands) gender: The gender of the individuals that...
Briefly outline the role of data preparation and data integration in the overall process of data...
Briefly outline the role of data preparation and data integration in the overall process of data management.
ARTICLE :Big data and big intelligence . “The big data revolution is not about the data....
ARTICLE :Big data and big intelligence . “The big data revolution is not about the data. It’s about the analytics that we can come up with and that we now have to beable to understand what these data say.” – Gary King, Professor and director of the Institute for Quantitative Social Science, Harvard University Companies need to collect, aggregate and analyze data to make better business decisions. With the help of business-intelligence tools and methodologies, companies can now analyze large...
A. Provides data independence B. Encourages data sharing C. Minimizes data redundancy D. Increases data inconsistency...
A. Provides data independence B. Encourages data sharing C. Minimizes data redundancy D. Increases data inconsistency Which of the following statements about database systems is (are) true? (Check all that apply.)
1.    What is Big Data? Why Is Big Data Different? (from data mart, data warehouse) 2.    What Are...
1.    What is Big Data? Why Is Big Data Different? (from data mart, data warehouse) 2.    What Are the Benefits of Big Data? 3.    Some of the potential business benefits from implementing an effective big data analytics 4.    How can organization leverage Big Data? For example, Big Data can be used to develop the next generation of products and services. For instance, manufacturers are using data obtained from sensors embedded in products to create innovative after-sales service offerings such as proactive maintenance to avoid...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT