SlideShare une entreprise Scribd logo
1  sur  20
K.K. WAGH POLYTECHNIC,
NASHIK-03
DEPARTMENT OF COMPUTER
TECHNOLOGY(Second Shift)
Demonstration of
Address Calculation
Sort
Bucket sort, or bin sort, is a distribution sorting algorithm.
It is a stable sort, where the relative order of any two items
with the same key is preserved.
It works in the following way:
 set up m buckets where each bucket is responsible for an equal
portion of the range of keys in the array.
 place items in appropriate buckets.
 sort items in each non-empty bucket using insertion sort.
 concatenate sorted lists of items from buckets to get final sorted
order.
Analysis of running time of Bucket sort:
 Best Case Complexity: O(n logn)
 Worst Case Complexity: O(n2)
Bucket Sort
Example of Bucket Sort
The example uses an input array of 9 elements. Key values are in the
range from 10 to 19. It uses an auxiliary array of linked lists which is
used as buckets.
Items are placed in appropriate buckets and links are maintained to
point to the next element. Order of the two keys with value 15 is
maintained after sorting.
n ← length [A]
For i = 1 to n do
Insert A[i] into list B[nA[i]]
For i = 0 to n-1 do
Sort list B with Insertion sort
Concatenate the lists B[0], B[1], . . B[n-1]
together in order.
Algorithm of Bucket Sort
Program for Bucket Sort
#include<stdio.h>
void Bucket_Sort(int array[], int n)
{
int i, j,k;
int count[10][50];
for(i=0; i < 10; i++)
{
for(j=0;j<50;j++)
count[i][j] = 0;
}
for(i=0,j=0; i < n; i++)
{ k=array[i]/10;
j++;
count[k][j]=array[i];
printf("ncount[%d][%d]=%d",k,j,array[i]);
}
for(i=0,j=0,k=0; i < n; i++)
{
for(j=0;j<50;j++)
{
if(count[i][j]>0)
array[k++] = count[i][j];
}
}
}
int main()
{
int array[100]; int num; int i;
clrscr();
printf("Enter How many Numbers : ");
scanf("%d",&num);
Program for Bucket Sort
printf("Enter the %d elements to be sorted:n",num);
for(i = 0; i < num; i++ )
{
scanf("%d",&array[i]);
}
printf("nThe array of elements before sorting : n");
for (i = 0;i < num;i++)
{
printf("%d ", array[i]);
}
printf("nThe array of elements after sorting :n");
Bucket_Sort(array, num);
for (i = 0;i < num;i++)
{
printf("%d ", array[i]);
}
return 0;
}
Program for Bucket Sort
Radix Sort
Radix is the base of a number system or logarithm.
Radix sort is a multiple pass distribution sort.
 It distributes each item to a bucket according to part of
the item's key.
 After each pass, items are collected from the buckets,
keeping the items in order, then redistributed according
to the next most significant part of the key.
This sorts keys digit-by-digit (hence referred to as digital
sort), or, if the keys are strings that we want to sort
alphabetically, it sorts character-by-character.
Radix sort uses bucket or count sort as the stable sorting
algorithm, where the initial relative order of equal keys is
unchanged.
Radix Sort
Radix sort is classified based on how it works internally:
least significant digit (LSD) radix sort:
Processing starts from the least significant digit and
moves towards the most significant digit.
Most significant digit (MSD) radix sort:
Processing starts from the most significant digit and
moves towards the least significant digit. This is recursive. It
works in the following way:
Example of LSD-Radix Sort
12 44 41 34 11 32 23
Input is an array of 15 integers. For integers, the number of buckets is 10, from 0 to 9.
The first pass distributes the keys into buckets by the least significant digit (LSD).
When the first pass is done, we have the following.
23
44
34
12
42
32
41
11
0 1 2 3 4 5 6 7 8 9
5087 77
77
50 87 58
58
08
0842
Example of LSD-Radix Sort…contd
50
We collect these, keeping their relative order:
Now we distribute by the next most significant digit, which is the highest digit in our
example, and we get the following.
11
12
23
32
34
41
42
44
When we collect them, they are in order.
12 42 444111 3223 34
12 42 4441 3411 32 23 77 58 08
0 1 2 3 4 5 6 7 8 9
50 877708
08 50 77 87
58
58
87
#include <conio.h>
#include <stdio.h>
void main()
{
int unsorted[50] , bucket[10][50]={{0}} , sorted[50] ;
int j , k , m , p , flag = 0, num, N;
clrscr();
printf("nEnter the number of elements to be sorted :");
scanf("%d",&N);
printf("nEnter the elements to be sorted :n");
for(k=0 ; k < N ; k++)
{
scanf("n%d",&num);
sorted[k] = unsorted[k] = num;
}
for(p=1; flag != N ; p*=10)
{
flag = 0;
for(k=0;k<N;k++)
{
printf("n flag=%d",flag);
bucket[(sorted[k]/p)%10][k] = sorted[k];
printf("n position of element=%d %d",((sorted[k]/p)%10),k);
printf("n element value=%d",bucket[(sorted[k]/p)%10][k]);
if ( (sorted[k]/p)%10 == 0 )
{
flag++;
}
}
for(j=0,m=0;j<10;j++)
{
for(k=0;k<N;k++)
{
if( bucket[j][k] > 0 )
{
sorted[m] = bucket[j][k];
bucket[j][k] = 0 ; m++;
}
}
}
}
if (flag == N)
{
printf("nSorted List: n");
for(j=0 ; j < N ; j++)
{
printf("%dt", sorted[j]);
} printf("n");
}
getch() ;
}
 This can be one of the fastest types of distributive sorting
technique if enough space is available also called as
Hashing.
 In this algorithm, a hash function is used and applied to
each element in the list. The result of the hash function
is placed in to an address in the table that represents the
key.
 Linked lists are used as address table for storing keys.(if
there are 4 keys then 4 linked lists are used).
 The hash function places the elements in linked lists are
called as sub files. An item is placed into a sub-file in
correct sequence by using any sorting method. After all
the elements are placed into subfiles, the lists
(subfies)are concatenated to produce the sorted list.
Address Calculation Sort
Procedure:
1. In this method a hash function f is applied to each key.
2. The result of this function determines into which of the
several subfiles the record is to be placed.
The function should have the property that: if x <= y ,
f (x) <= f (y), Such a function is called order preserving.
3. An item is placed into a subfile in correct sequence by
using any sorting method – simple insertion is often
used.
4. After all the elements are placed into subfiles, the lists
(subfiles) are concatenated to produce the sorted list.
Address Calculation Sort
Example:
25 57 48 37 12 92 86 33
Let us create 10 subfiles (linked lists). Initially each of
these subfiles is empty.
Key.
Address Calculation Sort
0
1
2
3
4
5
6
7
8
9
Example:
The number is passed to hash function, which returns its last
digit (ten’s place digit), which is placed at that position only,
in the linked lists.
num= 25 - f(25) gives 2
57 - f(57) gives 5
48 - f(48) gives 4
37 - f(37) gives 3
12 - f(12) gives 1
92 - f(92) gives 9
86 - f(86) gives 8
33 - f(33) gives 3
which is repeated.
Address Calculation Sort
0
1
2
3
4
5
6
7
8
9
25
57
48
33
12
92
86
37
Example:
After concatenating all linked list we get the sorted list.
12 25 33 37 48 57 86 92
Efficiency:
If all the elements (n) are uniformly distributed over m
subfiles then n/m is approximately 1, time of the sort is near
O(n).
On the other hand if maximum elements accommodate in
one or two subfiles, then n/m is much larger significant work
is required to insert element into proper subfile at its proper
position and efficiency is O(n2).
Address Calculation Sort
Thank You

Contenu connexe

Tendances (20)

COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Hashing
HashingHashing
Hashing
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
8 queen problem
8 queen problem8 queen problem
8 queen problem
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Huffman coding
Huffman coding Huffman coding
Huffman coding
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Sorting
SortingSorting
Sorting
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queues
QueuesQueues
Queues
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 

En vedette

Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilersVasim Pathan
 
Webinar – Pricing & Regulations: Will you be able to justify your valuations ...
Webinar – Pricing & Regulations: Will you be able to justify your valuations ...Webinar – Pricing & Regulations: Will you be able to justify your valuations ...
Webinar – Pricing & Regulations: Will you be able to justify your valuations ...aimsoftware
 
Universities as Regenerators: Rupert Goddard, Sheppard Robson
Universities as Regenerators: Rupert Goddard, Sheppard RobsonUniversities as Regenerators: Rupert Goddard, Sheppard Robson
Universities as Regenerators: Rupert Goddard, Sheppard RobsonPlace North West
 
Promotional Strategies: Virgin Mobile India
Promotional Strategies: Virgin Mobile IndiaPromotional Strategies: Virgin Mobile India
Promotional Strategies: Virgin Mobile Indiashruthi.rajan
 
Power Of Promotional Products
Power Of Promotional ProductsPower Of Promotional Products
Power Of Promotional Productsguest71c04f
 
Time Management Strategies For Business Owners
Time Management Strategies For Business OwnersTime Management Strategies For Business Owners
Time Management Strategies For Business OwnersSynduit
 
Options pricing ( calculation of option premium)
Options pricing ( calculation of option premium)Options pricing ( calculation of option premium)
Options pricing ( calculation of option premium)Ameya Ranadive
 
PHARMA Target Segmentation Grid (2 plus 2)
PHARMA Target Segmentation Grid (2 plus 2)PHARMA Target Segmentation Grid (2 plus 2)
PHARMA Target Segmentation Grid (2 plus 2)Walid Saafan
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
Chapter 4 - Marketing Research Process
Chapter 4 - Marketing Research ProcessChapter 4 - Marketing Research Process
Chapter 4 - Marketing Research ProcessDr. Ankit Kesharwani
 
A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...
A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...
A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...Rod King, Ph.D.
 

En vedette (18)

Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
 
Marketing fifthp
Marketing fifthpMarketing fifthp
Marketing fifthp
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Webinar – Pricing & Regulations: Will you be able to justify your valuations ...
Webinar – Pricing & Regulations: Will you be able to justify your valuations ...Webinar – Pricing & Regulations: Will you be able to justify your valuations ...
Webinar – Pricing & Regulations: Will you be able to justify your valuations ...
 
Product strategy
Product strategyProduct strategy
Product strategy
 
Chpt6
Chpt6Chpt6
Chpt6
 
Universities as Regenerators: Rupert Goddard, Sheppard Robson
Universities as Regenerators: Rupert Goddard, Sheppard RobsonUniversities as Regenerators: Rupert Goddard, Sheppard Robson
Universities as Regenerators: Rupert Goddard, Sheppard Robson
 
Promotional Strategies: Virgin Mobile India
Promotional Strategies: Virgin Mobile IndiaPromotional Strategies: Virgin Mobile India
Promotional Strategies: Virgin Mobile India
 
Power Of Promotional Products
Power Of Promotional ProductsPower Of Promotional Products
Power Of Promotional Products
 
Time Management Strategies For Business Owners
Time Management Strategies For Business OwnersTime Management Strategies For Business Owners
Time Management Strategies For Business Owners
 
Options pricing ( calculation of option premium)
Options pricing ( calculation of option premium)Options pricing ( calculation of option premium)
Options pricing ( calculation of option premium)
 
PHARMA Target Segmentation Grid (2 plus 2)
PHARMA Target Segmentation Grid (2 plus 2)PHARMA Target Segmentation Grid (2 plus 2)
PHARMA Target Segmentation Grid (2 plus 2)
 
Basics of brand map
Basics of brand mapBasics of brand map
Basics of brand map
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Brand mapping tool
Brand mapping toolBrand mapping tool
Brand mapping tool
 
Chapter 4 - Marketing Research Process
Chapter 4 - Marketing Research ProcessChapter 4 - Marketing Research Process
Chapter 4 - Marketing Research Process
 
A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...
A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...
A New Way of Looking at Eric Ries's Vision-Strategy-Product (VSP) Pyramid: TH...
 

Similaire à Address calculation-sort

CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfsaneshgamerz
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringRAJASEKHARV8
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Searchecomputernotes
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 
computer notes - Data Structures - 32
computer notes - Data Structures - 32computer notes - Data Structures - 32
computer notes - Data Structures - 32ecomputernotes
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptxreddy19841
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 

Similaire à Address calculation-sort (20)

CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithm
 
Merge radix-sort-algorithm
Merge radix-sort-algorithmMerge radix-sort-algorithm
Merge radix-sort-algorithm
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Search
 
Data structure
Data  structureData  structure
Data structure
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
computer notes - Data Structures - 32
computer notes - Data Structures - 32computer notes - Data Structures - 32
computer notes - Data Structures - 32
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
lecture 10
lecture 10lecture 10
lecture 10
 
Arrays
ArraysArrays
Arrays
 
Searching
SearchingSearching
Searching
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
Lec4
Lec4Lec4
Lec4
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 

Dernier (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

Address calculation-sort

  • 1. K.K. WAGH POLYTECHNIC, NASHIK-03 DEPARTMENT OF COMPUTER TECHNOLOGY(Second Shift) Demonstration of Address Calculation Sort
  • 2. Bucket sort, or bin sort, is a distribution sorting algorithm. It is a stable sort, where the relative order of any two items with the same key is preserved. It works in the following way:  set up m buckets where each bucket is responsible for an equal portion of the range of keys in the array.  place items in appropriate buckets.  sort items in each non-empty bucket using insertion sort.  concatenate sorted lists of items from buckets to get final sorted order. Analysis of running time of Bucket sort:  Best Case Complexity: O(n logn)  Worst Case Complexity: O(n2) Bucket Sort
  • 3. Example of Bucket Sort The example uses an input array of 9 elements. Key values are in the range from 10 to 19. It uses an auxiliary array of linked lists which is used as buckets. Items are placed in appropriate buckets and links are maintained to point to the next element. Order of the two keys with value 15 is maintained after sorting.
  • 4. n ← length [A] For i = 1 to n do Insert A[i] into list B[nA[i]] For i = 0 to n-1 do Sort list B with Insertion sort Concatenate the lists B[0], B[1], . . B[n-1] together in order. Algorithm of Bucket Sort
  • 5. Program for Bucket Sort #include<stdio.h> void Bucket_Sort(int array[], int n) { int i, j,k; int count[10][50]; for(i=0; i < 10; i++) { for(j=0;j<50;j++) count[i][j] = 0; } for(i=0,j=0; i < n; i++) { k=array[i]/10; j++; count[k][j]=array[i]; printf("ncount[%d][%d]=%d",k,j,array[i]); }
  • 6. for(i=0,j=0,k=0; i < n; i++) { for(j=0;j<50;j++) { if(count[i][j]>0) array[k++] = count[i][j]; } } } int main() { int array[100]; int num; int i; clrscr(); printf("Enter How many Numbers : "); scanf("%d",&num); Program for Bucket Sort
  • 7. printf("Enter the %d elements to be sorted:n",num); for(i = 0; i < num; i++ ) { scanf("%d",&array[i]); } printf("nThe array of elements before sorting : n"); for (i = 0;i < num;i++) { printf("%d ", array[i]); } printf("nThe array of elements after sorting :n"); Bucket_Sort(array, num); for (i = 0;i < num;i++) { printf("%d ", array[i]); } return 0; } Program for Bucket Sort
  • 8. Radix Sort Radix is the base of a number system or logarithm. Radix sort is a multiple pass distribution sort.  It distributes each item to a bucket according to part of the item's key.  After each pass, items are collected from the buckets, keeping the items in order, then redistributed according to the next most significant part of the key. This sorts keys digit-by-digit (hence referred to as digital sort), or, if the keys are strings that we want to sort alphabetically, it sorts character-by-character. Radix sort uses bucket or count sort as the stable sorting algorithm, where the initial relative order of equal keys is unchanged.
  • 9. Radix Sort Radix sort is classified based on how it works internally: least significant digit (LSD) radix sort: Processing starts from the least significant digit and moves towards the most significant digit. Most significant digit (MSD) radix sort: Processing starts from the most significant digit and moves towards the least significant digit. This is recursive. It works in the following way:
  • 10. Example of LSD-Radix Sort 12 44 41 34 11 32 23 Input is an array of 15 integers. For integers, the number of buckets is 10, from 0 to 9. The first pass distributes the keys into buckets by the least significant digit (LSD). When the first pass is done, we have the following. 23 44 34 12 42 32 41 11 0 1 2 3 4 5 6 7 8 9 5087 77 77 50 87 58 58 08 0842
  • 11. Example of LSD-Radix Sort…contd 50 We collect these, keeping their relative order: Now we distribute by the next most significant digit, which is the highest digit in our example, and we get the following. 11 12 23 32 34 41 42 44 When we collect them, they are in order. 12 42 444111 3223 34 12 42 4441 3411 32 23 77 58 08 0 1 2 3 4 5 6 7 8 9 50 877708 08 50 77 87 58 58 87
  • 12. #include <conio.h> #include <stdio.h> void main() { int unsorted[50] , bucket[10][50]={{0}} , sorted[50] ; int j , k , m , p , flag = 0, num, N; clrscr(); printf("nEnter the number of elements to be sorted :"); scanf("%d",&N); printf("nEnter the elements to be sorted :n"); for(k=0 ; k < N ; k++) { scanf("n%d",&num); sorted[k] = unsorted[k] = num; }
  • 13. for(p=1; flag != N ; p*=10) { flag = 0; for(k=0;k<N;k++) { printf("n flag=%d",flag); bucket[(sorted[k]/p)%10][k] = sorted[k]; printf("n position of element=%d %d",((sorted[k]/p)%10),k); printf("n element value=%d",bucket[(sorted[k]/p)%10][k]); if ( (sorted[k]/p)%10 == 0 ) { flag++; } }
  • 14. for(j=0,m=0;j<10;j++) { for(k=0;k<N;k++) { if( bucket[j][k] > 0 ) { sorted[m] = bucket[j][k]; bucket[j][k] = 0 ; m++; } } } } if (flag == N) { printf("nSorted List: n"); for(j=0 ; j < N ; j++) { printf("%dt", sorted[j]); } printf("n"); } getch() ; }
  • 15.  This can be one of the fastest types of distributive sorting technique if enough space is available also called as Hashing.  In this algorithm, a hash function is used and applied to each element in the list. The result of the hash function is placed in to an address in the table that represents the key.  Linked lists are used as address table for storing keys.(if there are 4 keys then 4 linked lists are used).  The hash function places the elements in linked lists are called as sub files. An item is placed into a sub-file in correct sequence by using any sorting method. After all the elements are placed into subfiles, the lists (subfies)are concatenated to produce the sorted list. Address Calculation Sort
  • 16. Procedure: 1. In this method a hash function f is applied to each key. 2. The result of this function determines into which of the several subfiles the record is to be placed. The function should have the property that: if x <= y , f (x) <= f (y), Such a function is called order preserving. 3. An item is placed into a subfile in correct sequence by using any sorting method – simple insertion is often used. 4. After all the elements are placed into subfiles, the lists (subfiles) are concatenated to produce the sorted list. Address Calculation Sort
  • 17. Example: 25 57 48 37 12 92 86 33 Let us create 10 subfiles (linked lists). Initially each of these subfiles is empty. Key. Address Calculation Sort 0 1 2 3 4 5 6 7 8 9
  • 18. Example: The number is passed to hash function, which returns its last digit (ten’s place digit), which is placed at that position only, in the linked lists. num= 25 - f(25) gives 2 57 - f(57) gives 5 48 - f(48) gives 4 37 - f(37) gives 3 12 - f(12) gives 1 92 - f(92) gives 9 86 - f(86) gives 8 33 - f(33) gives 3 which is repeated. Address Calculation Sort 0 1 2 3 4 5 6 7 8 9 25 57 48 33 12 92 86 37
  • 19. Example: After concatenating all linked list we get the sorted list. 12 25 33 37 48 57 86 92 Efficiency: If all the elements (n) are uniformly distributed over m subfiles then n/m is approximately 1, time of the sort is near O(n). On the other hand if maximum elements accommodate in one or two subfiles, then n/m is much larger significant work is required to insert element into proper subfile at its proper position and efficiency is O(n2). Address Calculation Sort