Sorting and Hashing: By, Pankti Doshi
Sorting and Hashing: By, Pankti Doshi
By,
Pankti Doshi
Quick Sort
• Known as quick sort or partition exchange sort.
• Consider x as array of integers of which the n integers are to
be sorted.
Quick Sort
• Working:
– Choose an element a from a specific position
within the array (for example, a can be chosen as
the first element so that a =x[0]).
– Partition elements of x so that a is placed into
position j and the following conditions hold:
1. Each of the elements in positions 0 through j-1 is less
than or equal to a.
2. Each of the elements in positions j+1 through n-1 is
greater than or equal to a.
• If above two conditions holds for a particular
a and j, a is the jth smallest element of x.
Quick Sort Working
Original array 25 57 48 37 12 92 86 33
Place first element 25 at its proper 12 25 57 48 37 92 86 33
position
Repeat the process for next sub array 12 25 57 48 37 92 86 33
Place first element 57 of sub array at 12 25 48 37 33 57 92 86
proper position.
Repeat the process for next sub arrays 12 25 37 33 48 57 92 86
Repeat the process for next sub arrays 12 25 33 37 48 57 92 86
Repeat the process for next sub arrays 12 25 33 37 48 57 86 92
Final array 12 25 33 37 48 57 86 92
40 20 10 80 60 50 7 30 100
Partitioning Array
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
down up
1. While data[down] <= data[pivot]
++down
pivot_index = 0 40 20 10 80 60 50 7 30 100
down up
1. While data[down] <= data[pivot]
++down
pivot_index = 0 40 20 10 80 60 50 7 30 100
down up
1. While data[down] <= data[pivot]
++down
pivot_index = 0 40 20 10 80 60 50 7 30 100
up
down
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
pivot_index = 0 40 20 10 80 60 50 7 30 100
up
down
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
pivot_index = 0 40 20 10 80 60 50 7 30 100
up
down
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
pivot_index = 0 40 20 10 80 60 50 7 30 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
pivot_index = 0 40 20 10 30 60 50 7 80 100
up
down
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
pivot_index = 0 40 20 10 30 60 50 7 80 100
pivot_index = 0 40 20 10 30 60 50 7 80 100
pivot_index = 0 40 20 10 30 60 50 7 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
down up
1. While data[down]<= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
pivot_index = 0 40 20 10 30 7 50 60 80 100
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
5. Swap data[up] and data[pivot_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
down up
1. While data[down] <= data[pivot]
++down
2. While data[up] > data[pivot]
--up
3. If down < up
swap data[down] and data[up]
4. While up > down, go to 1.
5. Swap data[up] and data[pivot_index]
pivot_index = 4 7 20 10 30 40 50 60 80 100
down up
Partition Result
7 20 10 30 40 50 60 80 100
7 20 10 30 40 50 60 80 100
Searching time:
1. Linear Search – O(n) – n number of elements
2. Binary Search – O(log n) – n number of elements
It is impractical to waste so much storage space just to ensure that each employee’s
record is in a unique and predictable location.
Hashing
• Whether we use a two-digit primary key (Emp_ID) or a five-digit key, there are just
100 employees in the company.
• Thus, we will be using only 100 locations in the array.
• Therefore, in order to keep the array size down to the size that we will actually
be using (100 elements), another good option is to use just the last two digits
of the key to identify each employee.
• For example, the employee with Emp_ID 79439 will be stored in the element of
the array with index 39.
• Emp_ID 12345 will have his record stored in the array at the 45th location.
• Requirement need to convert a five digit key number to a two digit array
index. We need function, which will do the conversion.
• In this case : array is known as hash table and function which will do the
conversion is known as hash function.
Hash tables • Hash table is a data
structure in which keys
are mapped to array
positions by a hash
function.
• Division Method
• Multiplication Method
• Mid-Square Method
• Folding Method
Division Method
• Determinism - Same hash value must get generated for given input value.
where (kA mod 1) gives the fractional part of kA and m is the total number of
indices in the hash table.
Given a hash table of size 1000, map the key 12345 to an appropriate location
in the hash table.
Solution We will use A = 0.618033, m = 1000, and k = 12345
h(12345) = |_ 1000 (12345 ¥ 0.618033 mod 1) _|
h(12345) = |_1000 (7629.617385 mod 1) _|
h(12345) = |_1000 (0.617385) _|
h(12345) = |_617.385_|
h(12345) = 617
Mid Square Method
The mid-square method is a good hash function which works in two steps:
Step 1: Square the value of the key. That is, find k2.
Step 2: Extract the middle r digits of the result obtained in Step 1.
In the mid-square method, the same r digits must be chosen from all the keys. Therefore, the hash
function can be given as:
h(k) = s
Calculate the hash value for keys 1234 and 5642 using the mid-square method.
The hash table has 100 memory locations.
Solution Note that the hash table has 100 memory locations whose indices vary from 0 to 99.
This means that only two digits are needed to map the key to a location in the hash table, so r = 2.
Step 1: Divide the key value into a number of parts. That is, divide k into parts
k1, k2, ..., kn, where each part has the same number of digits except the last
part which may have lesser digits than the other parts.
Step 2: Add the individual parts. That is, obtain the sum of k1 + k2 + ... + kn. The
hash value is produced by ignoring the last carry, if any
Given a hash table of 100 locations, calculate the hash value using folding
method for keys 5678, 321, and 34567.
Solution
Since there are 100 memory locations to address, we will break the key into parts
where each part (except the last) will contain two digits. The hash values can be
obtained as shown below:
Collisions
• Collisions occur when the hash function maps two different keys to the same
location.
• A method used to solve the problem of collision, also called collision resolution
technique
• If collision occurred
• Computes new positions using a probe sequence and the next record is stored in
that position.
• Hash function contains two types of values : senitinel values (e.g. -1) and data values.
• Senitinel value indicated – no value is stored at location at present, however value can
be stored.
• When a key is mapped to a particular memory location, then the value it holds is
checked.
• If it contains a sentinel value, then the location is free and the data value can be stored
in it.
• If the location already has some data value stored in it, then other slots are examined
systematically in the forward direction to find a free slot.
• If even a single free location is not found, then we have an OVERFLOW condition.
• The process of examining memory locations in the hash table is called probing.
Collision resolution by open addressing
Where m is the size of the hash table, h’(k) = (k mod m), and i
is the probe number that varies from 0 to m–1.
Code to initialize:
void intitializehashtable(node *hashtable[], int m)
{
int I;
for(i=0;i<=m;i++)
hashtable[i]=NULL;
}
Code to insert a value into hash table
node *insert_value( node *hash_table[], int val)
{
node *new_node;
new_node = (node *)malloc(sizeof(node));
new_node-> value = val;
new_node-> next = val;
hashtable[h(x)] = new_node;
}
Code to search a value into hash table
node *search_value(node *hashtable[], int val)
{
node *ptr;
ptr = hashtable[h(x)];
while ( (ptr!=NULL) && (ptr –> value != val))
ptr = ptr –> next;
if (ptr–>value == val)
return ptr;
else
return NULL;
}
Code to delete a value from hash table
void delete_value (node *hashtable[], int val)
{
node *save, *ptr;
save = NULL;
ptr = hashtable[h(x)];
while ((ptr != NULL) && (ptr value != val))
{
save = ptr;
ptr = ptr next;
}
if (ptr != NULL)
{
save next = ptr next;
free (ptr);
}
else
printf("\n VALUE NOT FOUND");
}
Example of chained hash table
Insert the keys 7, 24, 18, 52, 36, 54, 11, and 23 in a chained hash table of 9 memory
locations.
In this case, m=9. Initially, the hash table can be given as:
Step 1 Key = 7
h(k) = 7 mod 9
=7
Create a linked list for location 7 and
store the key value 7 in it as its only
node.
Example of chained hash table
Insert the keys 7, 24, 18, 52, 36, 54, 11, and 23 in a chained hash table of 9 memory
locations.