table/ sort/ tree

本文介绍了哈希表的概念,包括其作用、优点和潜在的冲突问题。接着讨论了不同类型的排序算法,如冒泡排序、插入排序和选择排序,以及快速排序和归并排序等更高效的方法。最后,探讨了二叉树的基本概念,如二叉搜索树、AVL树和红黑树,并简要提到了图和图的表示方法。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值