Monday, January 27, 2020

Increasing Time Efficiency of Insertion Sort

Increasing Time Efficiency of Insertion Sort Increasing Time Efficiency of Insertion Sort for the Worst Case Scenario Surabhi Patel, Moirangthem Dennis Singh Abstract. Insertion sort gives us a time complexity of O(n) for the best case. In the worst case where the input is in the descending order fashion, the time complexity is O(n2). In the case of arrays, shifting is taking O(n2) while in the case of linked lists, comparison is coming to O(n2). Here a new way of sorting for the worst case problem is proposed. We will use arrays as data structures and take more space. We will take 2n spaces where n is the number of elements and start the insertion from (n-1)th location of the array. In this proposed technique the time complexity is O(nlogn) as compared to O(n2) in the worst case. Keywords. Insertion Sort, Time Complexity, Space Complexity Introduction Insertion sort is a simple sorting algorithm[1], a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Every repetition of insertion sort removes an element from the input data, inserting it into the correct position in the already-sorted list, until no input elements remain. The best case input is an array that is already sorted. In this case insertion sort has a linear running time which is O(n). During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection of the array. The worst case input is an array sorted in reverse order. In this case, every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. For this case insertion sort has a quadratic running time which is O(n2). The average case also has a quadratic running time of O(n2). Literature Survey In an insertion sort algorithm, there are always two constraints in time complexity. One is shifting the elements and the other one is comparison of the elements. The time complexity is also dependent on the data structure which is used while sorting. If we use array as data structure then shifting takes O(n2) in the worst case. While using link list data structure, searching takes more time, viz. O(n2). We will take the following examples: Sort 50, 40, 30, 20, 10 using arrays. Shifting = 0, Comparison = 0 Shifting = 1, Comparison = log1 Shifting = 2, Comparison = log2 Shifting = 3, Comparison = log3 Shifting = 4, Comparison = log4 Time Complexity in Shifting: O(n2) Time Complexity in Comparison: O(nlogn) Total time complexity: O(n2) Here as the array is sorted, we can use binary search for comparison which will lead to a time complexity of O(nlogn) but Shifting takes O(n2). Therefore the total time complexity becomes O(n2) To solve this problem, link list can be used as illustrated in the following example. Sort 50, 40, 30, 20, 10 using link list. In a link list shifting takes O(1) as new elements can be inserted at their right positions without shifting. Comparison = 0 Comparison = 1 Comparison = 2 Comparison = 3 Comparison = 4 Time Complexity in Shifting: O(1) Time Complexity in Comparison: O(n2) Total time Complexity: O(n2) Here as we cannot use binary search for comparison which will lead to a time complexity O(n2) even though shifting takes a constant amount of time. As we have observed in the examples illustrated above, in both the cases the Time complexity is not getting reduced. Hence we are proposing an improvised insertion sort taking additional space to sort the elements. As space complexity is less important than time complexity[2][3], we have concentrated more over the time taken instead of space. Proposed Work In the insertion sort technique proposed here, we will take 2n spaces in an array data structure, where n is the total number of elements. The insertion of elements will start from n-1th position of the array. The same procedure of a standard insertion sort is followed in this technique. Finding the suitable positions of the elements to be inserted will be done using binary search. In the following cases we will discuss the details of our work. Case 1 For the best case scenario in a standard Insertion Sort is the input elements in ascending order using proposed technique. e.g. 10, 20, 30, 40, 50 Shifting =0 , Comparison = 0 Shifting =0 , Comparison = 1 Shifting =0 , Comparison = 1 Shifting =0 , Comparison = 1 Shifting =0 , Comparison = 1 Total Shifting =0, Total Comparison = n-1 Therefore time complexity is O(1)+O(n) = O(n) Case 2: For the worst case scenario in a standard Insertion Sort is the input elements in descending order using proposed technique. e.g. 50, 40, 30, 20, 10 Shifting =0 , Comparison = 0 Shifting =1 , Comparison = log1 Shifting =1 , Comparison = log2 Shifting =1 , Comparison = log3 Shifting =1 , Comparison = log4 Total Shifting =n-1, Total Comparison =log( 1*2*3*4) =log((n-1)!) =log((n-1) (n-1)) =(n-1)log(n-1) =nlog(n-1) log(n-1) Therefore time complexity is O(n)+O(nlogn) = O(nlogn) Case 3: For the average case scenario in a standard Insertion Sort, the input elements are in random order. We are following the same procedure but comparison is done via binary search algorithm. Hence it takes O(nlogn) for comparison. For shifting the elements, the time taken tends to O(n2) but is not equal to O(n2). As we have more spaces, there are possibilities that the shifting of some elements may be reduced because elements may be inserted both at the end as well as in the beginning. Results Now we compare the time complexities of proposed sorting technique and the standard Insertion sort. Conclusion We are decreasing the time complexity of worst case scenario in Insertion sort algorithm by increasing the space complexity. Our future scope of work includes decreasing time complexity of the average case which is O(n2) currently. There are promising results shown in the average case scenario where the time complexity may be reduce from O(n2), if the probability of the input elements is a combination of increasing and decreasing order. Acknowledgement We would like to thank Prof Anirban Roy, Department of Basic Sciences Christ University Faculty of Engineering for helpful discussions and support. REFERENCES Insertion Sort,http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Insertion_sort.html Michael A. Bender, â€Å"Insertion Sort is O(nlogn),† Third International Conference on Fun With Algorithms(FUN), Pages 16-23, 2004 H. W. Thimbleby, â€Å"Using Sentinels in Insert Sort,† Software Practice and Experience, Volume 19(3), Pages 303–307, 1989.

Sunday, January 19, 2020

Leadership Essay -- Leader Leadership

Leadership There are at least two things you can say about leadership without sounding stupid or exaggerating, 1) Leaders are important, and secondly 2) There is always a leadership crisis of one kind or another, at any given time; e.g. shortage or incompetence, evil, crooked, inadequate, you name it. As simplistic as these statements may sound, they are paramount to any study or discussion of leadership and its effectiveness. In this paper, I would like to elaborate upon some qualities of a good leader, the relationship between leaders and the followers, the difference between a leader and a manager, between leadership and authority, and finally touch upon the possible evils and misuse of power and leadership positions. We have all heard that leaders are born and not made. I do not agree with this statement in the least. While some leadership qualities are innate to all people, the most notable leaders are churned out after years of experience and practice of certain skill-sets. Effective leaders are rare. They are created through the trials and tribulations of managing or working with people in a variety of settings. Most good leaders have had their fair share of hard knocks that awakened them to the greater potential within their scope of influence. Effective leaders are excellent communicators, able to speak the many languages of the individuals within the groups they lead. A good leader pays attention to the facts and then makes decisions based on objective analysis of those facts. Effective leaders naturally motive people from their actions, which include showing respect, listening, reflecting, and negotiating through conflict. Leaders encourage and empower people to achieve success rather than place blame. Leaders ... ...isuse power because those with power feel morally superior. There is one school of thought that believes that power corrupts slowly and gradually, while some others believe that those who misuse their position, do so in bold strikes. Leaders inherently have power and influence over their followers. There have been instances in history where leaders are granted god-like status, with absolute right to making judgments. This is true even in some cases in today's world vis-Ã  -vis national and religious leaders of major faiths. Leaders can also take advantage of the general reluctance to take on responsibility and accountability. By being willing to take on those burdens, a charismatic leader can amass a range of powers, including those that are in conflict, such as the power both to propose and approve acquisitions or to execute and account for securities transactions.

Saturday, January 11, 2020

“a&P” and “Barn Burning”: a Compare and Contrast Essay”

Sammy from John Updike’s â€Å"A&P† and Sarty from William Faulkner’s â€Å"Barn Burning† are two great examples of young people raising their standards and doing what they believe is right. In â€Å"A&P†, Sammy is nineteen years old and works at a local grocery store named the A&P. His life changes the day he quits his job after defending three girls that are â€Å"called out† by Lengel, the manager, for breaking the unwritten dress code. In â€Å"Barn Burning†, Sarty is a ten year old boy who struggles between the fine line of right and wrong when his father, Abner Snopes, is put on trial for burning down a barn. When his father attempts to burn down another barn, Sarty takes charge and warns the owner. â€Å"A&P† and â€Å"Barn Burning† are short story classics that have many similarities as well as differences. While both Sammy and Sarty are dissatisfied with their figures of authority, the time periods in which they live are extremely different. A very noticeable similarity is the fact that both young men are dissatisfied with their authority figures. In the beginning of the stories, one will see that Sammy and Sarty are not like their main influences. Each young man has a mature understanding of their unfortunate lives and how they must break the mold to better themselves. Stokesie, Sammy’s friend and co-worker, is twenty two years old and married with two kids. His biggest dream is to become a manager of the A&P someday. Sammy describes Stokesie’s life as a perfect example of his future if he continues working at the A&P. To him the A&P is a dead end job; but in the eyes of his Lengel and his family it is an acceptable future. Lengel is the manager of A&P as well as the town’s preacher. He plays an important role as Sammy’s authority figure. When Sammy quits, Lengel tries to persuade him to stay and makes it very clear that leaving the A&P will be a huge mistake. This is an excellent reflection of Sammy’s authority figure and the close minded ways he must escape. Sarty’s major authority figure in â€Å"Barn Burning† is his father. Abner is a poor sharecropper who is bitter towards wealthy plantation owners and has a tendency to destroy their property. Sarty never justifies his father’s actions and is aware that if he allows things to remain the same, he will become a product of his environment. This is his motive for warning Abner’s next barn burning victim and his chance to move on with his life. The time periods and locations in which â€Å"A&P† and â€Å"Barn Burning† take place are very different. Sammy lives in a more favorable time in the United States than Sarty. â€Å"A&P† is set in a small town north of Boston, Massachusetts around 1960. At this time, the United States was the main military manufacturer and financial power in the capitalist world. The â€Å"Hippie Movement† just started and shocked many traditional families with a new way of living. Sarty lives in Mississippi about twenty years after the Civil War. Life was tough for Mississippians post Civil War. The Southern states were in debt and devastation from the war was everywhere. Although America was transforming into a more modern country in both stories, the time periods in which they live are completely opposite. Sammy and Sarty are two protagonists that have to mature beyond normal circumstances and experience the â€Å"real world† at tender ages. Each character is forced to grow up and refine themselves in their darkest hour. Sammy and Sarty are both dissatisfied with their authority figures; however, the time periods in which they live take place in different eras.

Friday, January 3, 2020

Why Is North at the Top of the Map

Most modern-day maps typically show an orientation with north at the top of the two-dimentional depiction. In other eras, different directions at the top were more prevalent, and all directions have been used by different societies and cultures to depict our world. The biggest factors that contribute to north being commonly placed at the top of a map include the invention of the compass and the understanding of magnetic north and the egocentricity of society, mainly in Europe. The Compass Magnetic North The discovery and use of the compass in Europe in the 1200-1500s may have greatly influenced many modern-day maps with north at the top. A compass points to magnetic north, and Europeans, like other cultures long before, noticed that the earth spins on an axis that is relatively pointed at the north star. That idea combined with the concept that when we look up we see the stars, contributed to north being placed up at the top of maps, with words and symbols being placed relative to that viewpoint. Egocentricity in Societies Egocentricity is having a view or perspective that revolves around you or your situation at the center. Thus, in cartography and geography, an egocentric society is one that places itself in either the center of a depiction of the world, or at the top. Information at the top of a map is commonly viewed as being both more visible and more significant. Since Europe was a powerhouse in the world, producing both heavy exploration and the printing press – it was instinctual for European mapmakers to put Europe (and the Northern Hemisphere) as the focus at the top of maps. Today Europe and North America remain dominant cultural and economic forces, producing and influencing many maps – showing the Northern Hemisphere at the top of the map. Different Orientations Most early maps, before the wide-spread use of the compass, placed east at the top. This is generally thought to be due to the fact that the sun rises in the east. It was the most consistent directional maker. Many cartographers show what they want to be the focus at the top of the map, and therefore, influence the orientation of the map. Many early Arab and Egyptian cartographers placed south at the top of the map because, having most of the world they knew of to the north of them, it drew the most attention to their area. Many early settlers of North America created maps with a west-east orientation that resulted from the direction that they primarily traveled and explored. Their own viewpoint greatly altered the orientation of their maps. In the history of mapmaking, the general rule of thumb is whoever made the map is probably at the center or the top of it. This rings mostly true for centuries of mapmaking, but has been greatly influenced as well with European cartographers’ discovery of compasses and the magnetic north.