SlideShare ist ein Scribd-Unternehmen logo
1 von 1
Name: Nguyen Thi Nha Trang
ID: IT060052
                                     ASSIGNMENT

        Why the size of the array of a hash table should be a prime number.


We have:

                       Arrayindex = hugeNumber % arraySize
Where:
  • Arrayindex is the array index where the object is stored.
  • hugeNumber is the hash value of the object to be stored.
  • arraySize is the length of the array of the hash table.


The reason why array’s size of a hash table should be a prime number is that it can help
to reduce the collisions.
To make it easier to understand, lets suppose that if the array’s size is not a prime
number, then the remainder of the modulo operation will return 0 for all hash values
which are divisible by the array’s size. This will cause collisions which would force the
hash table to switch on the collision resolution logic which is very complex. It also will
make the hash table to call Equals function to all previously stored objects with the same
hash value to find out if the same object was already stored. Therefore, the array’s size
should be a prime number to help avoiding the collisions after the operating of the
modulo function.

Weitere ähnliche Inhalte

Was ist angesagt?

Arrays introduction- JAVA
Arrays introduction- JAVAArrays introduction- JAVA
Arrays introduction- JAVAHamna_sheikh
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrayschauhankapil
 
Types of Arrays
Types of ArraysTypes of Arrays
Types of ArraysAns Ali
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Workshop 04 Review
Workshop 04 ReviewWorkshop 04 Review
Workshop 04 Reviewmigiwara
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithmmaamir farooq
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search AlgorithmFazalRehman79
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Abbott
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithmAamir Sohail
 

Was ist angesagt? (20)

Arrays introduction- JAVA
Arrays introduction- JAVAArrays introduction- JAVA
Arrays introduction- JAVA
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Data structure
Data structureData structure
Data structure
 
Types of Arrays
Types of ArraysTypes of Arrays
Types of Arrays
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Cliff tip indexmatch_01
Cliff tip indexmatch_01Cliff tip indexmatch_01
Cliff tip indexmatch_01
 
Array
ArrayArray
Array
 
Data structures Lecture 5
Data structures Lecture 5Data structures Lecture 5
Data structures Lecture 5
 
Workshop 04 Review
Workshop 04 ReviewWorkshop 04 Review
Workshop 04 Review
 
Learn Java Part 9
Learn Java Part 9Learn Java Part 9
Learn Java Part 9
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Learn Java Part 8
Learn Java Part 8Learn Java Part 8
Learn Java Part 8
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Quick sort
Quick sortQuick sort
Quick sort
 
Data structures
Data structuresData structures
Data structures
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 

Ähnlich wie Assignment Algo

computer notes - Data Structures - 37
computer notes - Data Structures - 37computer notes - Data Structures - 37
computer notes - Data Structures - 37ecomputernotes
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithmfarhankhan89766
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesswathirajstar
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structureMahmoud Alfarra
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing TablesChinmaya M. N
 
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfAssignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfkksrivastava1
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxSLekshmiNair
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptxskilljiolms
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxunit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxBabaShaikh3
 

Ähnlich wie Assignment Algo (20)

computer notes - Data Structures - 37
computer notes - Data Structures - 37computer notes - Data Structures - 37
computer notes - Data Structures - 37
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 
Array
ArrayArray
Array
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
 
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfAssignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
 
Hashing
HashingHashing
Hashing
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Hashing
HashingHashing
Hashing
 
Hash based inventory system
Hash based inventory systemHash based inventory system
Hash based inventory system
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Hash pre
Hash preHash pre
Hash pre
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptx
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Arrays
ArraysArrays
Arrays
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxunit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
 

Assignment Algo

  • 1. Name: Nguyen Thi Nha Trang ID: IT060052 ASSIGNMENT Why the size of the array of a hash table should be a prime number. We have: Arrayindex = hugeNumber % arraySize Where: • Arrayindex is the array index where the object is stored. • hugeNumber is the hash value of the object to be stored. • arraySize is the length of the array of the hash table. The reason why array’s size of a hash table should be a prime number is that it can help to reduce the collisions. To make it easier to understand, lets suppose that if the array’s size is not a prime number, then the remainder of the modulo operation will return 0 for all hash values which are divisible by the array’s size. This will cause collisions which would force the hash table to switch on the collision resolution logic which is very complex. It also will make the hash table to call Equals function to all previously stored objects with the same hash value to find out if the same object was already stored. Therefore, the array’s size should be a prime number to help avoiding the collisions after the operating of the modulo function.