HashTable
Abstract datatype
faster data retrieval,
encrypt and decrypt digital signatures
a technique that is used to uniquely identify a specific object from a group of similar objects.
A hash table uses the key of each record to determine the location in an array structure. To do this, the key is passed into a hash function which will then return a numeric value based on the key.
a good hash function should be:
uniform (all indices are equally likely for the given set of possible keys)
random (not predictable)
Load Factor
number of records in table/ number of locations
Collisions
A hash function translates all possible keys into indexes in an array. This typically means that there are many many more keys than there are indexes. Thus, it is always possible that two or more keys will be translated into exactly the same index. When this happens you have a collision. Generally speaking, collisions are unavoidable.
Bucketing makes the hash table a 2D array instead of a single dimensional array. Every entry in the array is big enough to hold N items (N is not amount of data. Just a constant).
Problems:Lots of wasted space.|If N is exceeded, another strategy will need to be used|Not good for memory based implementations but doable if buckets are disk-based)
Chaining
At every location (hash index) in your hash table store a linked list of items.
For chaining, the runtimes depends on the load factor.The average length of each chain is λ.
Linear probing:single dimentional array.
bubble sort algorithm repeated bubbles the largest item within the list to the back.
voidbubbleSort(intarr[],intsize){
inttmp;inti;intj;
for(i=0;i<size-1;i++){
for(j=0;j<size-1-i;j++){
if(arr[j+1]<arr[j]){
tmp =arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;}}}}
Insertion sort: the algorithm repeated inserts a value into an array that is already sorted. It essentially chops the array into two pieces. The first piece is sorted, the second is not. We repeatedly take a number from the second piece and insert it into the already sorted first piece of the array.
voidinsertionSort(intarr[],intsize){
intcurr;inti,j;
for(i=1;i<size;i++){
curr=arr[i];
for(j=i;j>0&&arr[j-1]>curr;j–){
arr[j]=arr[j-1];}
arr[j]=curr;}}
Selection sort: selecting the smallest value out of the unsorted part of the array and placing it at the back of the sorted part of the array.
void selectionSort(int arr[],int size){
int i,j,min;
for(int i=0;i<size-1;i++){
min=i;
for(int j=i+1;j<size;j++){
if(arr[j] < arr[min]){
min=j;}}
if(min!=i){
int tmp=arr[min];
arr[min]=arr[i];
arr[i]=tmp;}}}
The merge sort works on the idea of merging two already sorted lists. If there existed two already sorted list, merging the two together into a single sorted list can be accomplished in O(n) time.
• Have a way to “point” at the first element of each of the two list
• compare the values being pointed at and pick the smaller of the two
• copy the smaller to the merged list, and ad
table/ sort/ tree
最新推荐文章于 2022-12-27 13:51:36 发布
本文介绍了哈希表的概念,包括其作用、优点和潜在的冲突问题。接着讨论了不同类型的排序算法,如冒泡排序、插入排序和选择排序,以及快速排序和归并排序等更高效的方法。最后,探讨了二叉树的基本概念,如二叉搜索树、AVL树和红黑树,并简要提到了图和图的表示方法。
摘要由CSDN通过智能技术生成