0% found this document useful (0 votes)
32 views

Team 7 Finding The KTH Smallest Element D11

1. The document presents two algorithms for finding the kth smallest element in an array: using a min-heap and using the standard nth element algorithm. 2. The min-heap algorithm builds a min-heap from the input array and repeatedly removes the minimum element, locating the kth smallest element after k-1 removals. 3. The standard nth element algorithm sorts the array and returns the element at index k-1.

Uploaded by

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

Team 7 Finding The KTH Smallest Element D11

1. The document presents two algorithms for finding the kth smallest element in an array: using a min-heap and using the standard nth element algorithm. 2. The min-heap algorithm builds a min-heap from the input array and repeatedly removes the minimum element, locating the kth smallest element after k-1 removals. 3. The standard nth element algorithm sorts the array and returns the element at index k-1.

Uploaded by

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

Finding the kth

smallest element
Presented by Team 7

Team members:
1. DEEPAK KUMAR (20BCE10660)
2. DEVANSH ASHOKKUMAR JAIN
(20BCE10663)

...
3. DIPTANU SAHA (20BCE10664)
4. ABHIMAN GAUTAM (20BCE10668)
5. AYUSH (20BCE10671)
Brief Description on ARRAYS
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make
use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.
❏ Element − Each item stored in an array is called an element.
❏ Index − Each location of an element in an array has a numerical index, which is used to identify the element.

ARRAY Representation
To create an array of numeric values in Python, we need to import the array module. For example:
INPUT: import array as arr
a = arr.array('i', [1, 3, 5])
print(a)

OUTPUT: array(‘i’, [1, 3, 5])

Here, we created an array of int type. The letter ‘i’ is a type code. This determines the type of the array during creation.
Basic Operations
Following are the basic operations supported by an array:
❏ Traverse − Prints all the array elements one by one.
❏ Insertion − Adds an element at the given index.
❏ Deletion − Deletes an element at the given index.
❏ Search − Searches an element using the given index or by the value.
❏ Update − Updates an element at the given index.

Accessing Array Elements


We can access each element of an array using the index of the element by using the below mentioned code:
INPUT: from array import *
array = array('i', [10,20,30,40,50])
print (array[0])
print (array[2])
When we compile and execute the above program, it produces the following result:
OUTPUT: 10
30
1. Using min-heap
❏ PROGRAM
Input: import heapq
from heapq import heappop

❏ ALGORITHM # Function to find the k’th smallest element in


# the list using min-heap
def kth_smallest(A, k):
1. Start.
2. Create a min-heap using the given array. # transform the input list into a min-heap
3. Use the listed array. heapq.heapify(A)
4. Remove the element at the root of the # pop from the min-heap exactly (k-1) times
while k > 1:
heap (k-1) times.
heappop(A)
5. The element on the root of the heap is k = k - 1
the kth smallest element. # return the root of min-heap
6. End. return A[0]

if __name__ == '__main__':
A = [7, 4, 6, 3, 9, 1]
k = 3
print("K'th smallest element is", kth_smallest(A, k))

Output: ‘K’th smallest element is 4


2. Using standard nth element
❏ PROGRAM
❏ ALGORITHM Input:
# Python program to find k'th smallest element
# Function to return k'th smallest
# element in a given array
1. Start. def kth_smallest(arr, n, k):
2. Create a user defined function(kth_smallest) and
pass three arguments(arr, n, k). # Sort the given array
arr.sort()
3. Now sort the array
4. Check if __name__==’__main__’ # Return k'th element in the sorted array
5. Now take an input of array as arr=[7, 4, 6, 3, 9, 1] return arr[k-1]
6. Assign n to the length of arr.
# Driver code
7. Assign k = 2 if __name__=='__main__':
8. Display kth smallest element by calling function arr = [7, 4, 6, 3, 9, 1]
n = len(arr)
[kth_smalest(arr,n,k)]
k = 3
print("K'th smallest element is",
kthSmallest(arr, n, k))

Output:
‘K’th smallest element is 4
Conclusion
At last, this presentation gives all the information about how can we find the kth smallest element from an
array. Arrays allow random access to elements. This makes accessing elements by position faster. It has
better cache locality that can make a pretty big difference in performance. Arrays represent multiple data
items of the same type using a single name while on the other hand we can’t change the size i.e. once we
have declared the array we can’t change its size because of static memory allocated to it. Here Insertion and
deletion are difficult as the elements are stored in consecutive memory locations and the shifting operation is
costly too.

Knowing the number of steps how long an operation step is one of the fundamental levels of understanding
how array data structures work. Picking the right data structure of array with an understanding of operations
allows you to craft code that takes fewer steps, which means greater efficiency.
Individual Contributions
1. Deepak Kumar (20BCE10660) - Research & Content Provider
2. Devansh Ashokkumar (20BCE10663) - Content Provider & Slides Making
3. Diptanu Saha (20BCE10664) - Slides Making & Slides Filtering
4. Abhiman Gautam (20BCE10668) - Content Provider & Slides Filtering
5. Ayush (20BCE10671) - Research & Content Provider

THANK

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy