学习笔记

  1.在C#语言中,String 对象是不可变,这就意味着每次对象进行改变时都要创建一个新的对象副本来保存数值;而StringBuffer对象是可变的,当对StringBuffer进行改变时,正在改变的就是原始对象而不是对副本进行操作。当创建长字符串,或者对相同对象进行许多改变,那么久应该用StringBuffer。


        2.ADO.NET和ADO存在着本质的区别,尽管他们的名字相似。ADO.NET基于一种断开连接的体系结构,并紧密集成了XML,特别适合开发松散耦合的解决方案。


        3.Integrated Security(集成安全)是连接到SQL SERVER最安全的方式,所以应该尽量使用。集成安全使用身份来执行代码,而不是通过连接字符串中的显式用户ID和密码来验证对数据库的访问。集成安全避免了再连接字符串中存储用户名和密码。为了再连接字符串中使用集成安全,可以把Integrated Security的值指定为SSPI或true:  如下                     Integrated Security=SSPI


         在应用程序配置文件写了连接字符串后,需要引用using System.Configuration才能在C#代码中使用
      string sqlconnection=ConfigurationManager.ConnectionString["name"].Tostring()来获取连接字符串

一种显而易见但明显不好的存储连接字符串的方法是把他们直接写在应用程序中。尽管这种方式具有良好的性能,但它的灵活性很差。如果连接字符串由于任何原因发生了改变,应用程序就需要重新编译,其安全性也很差。


    冒泡排序:两两之间进行比较,左边的要是比右边的大就交换   
   for(int i=0;i<sort.Length;i++)                     //控制趟数,每走一趟,内层循环需走次数就减少一次
       {                                               //因为每一趟走完,那一趟中最小数就到了最后
          for (int j = 0; j < sort.Length - i - 1; j++)  //控制次数,sort.Length - i还减1,否则会数组溢出
             {                                          //因为sort[j+1],所以得比数组长度小1
                 if(sort[j]>sort[j+1])
                  {
                       int temp=sort[j];
                       sort[j]=sort[j+1];
                       sort[j + 1] = temp;
                  }
              }
      }




    选择排序:每一趟,都是把最小的那个数放在最前面,里层循环找到在那一趟中存有最小数的数组下标赋给min,所以,
每一趟都是交换本趟的第一个数和最小的那个数的位置,其他的不动。
  for (i = 0; i < a.Length - 1; i++)
            {
                min = i;
                for (j = i + 1; j < a.Length; j++)
                {
                    if (a[j] < a[min])
                        min = j;              //找出最小数的下标赋给min
                }                             //交换本趟的第一个数和最小的那个数的位置,其他的不动
                temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }


   插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列
对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place
排序,因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。
  void InsertSort(char array[],unsigned int n)
  {
    int i,j;
    int temp;
    for(i=1;i<n;i++)
    {
      temp = array[i];                             //store the original sorted array in temp
      for(j=i ; j>0 && temp < array[j-1] ; j--)   //compare the new array with temp(maybe -1?)
      {
          array[j]=array[j-1];             //all larger elements are moved one pot to the right
      }
      array[j]=temp;
    }
 }


对半查找:
int   BinarySeach(elemtype   s[],keytype   key,int   n) 
{   int   low,high,mid; 
    low=0; 
    high=n-1; 
    while(low <=high) 
    { 
      mid=(low+high)/2; 
      if(s[mid].key==key)return   mid; 
      else   if(s[mid].key <key)   low=mid+1; 
      else   high=mid-1; 
    }   
    return   -1; 
}


简单插入排序实例:
把表分成两部分,前半部分已排序,后半部分未排序,我用|分开
初始为 5 | 1 7 3 1 6
一次插入排序,把第一个1插入前边已排序部分,得
1 5 | 7 3 1 6
后边依次是
1 5 7 | 3 1 6
1 3 5 7 | 1 6
1 1 3 5 7 | 6
1 1 3 5 6 7 |




[算法问题]合并两个已经排序的数组为另一个数组


#include <stdio.h>
 
void DisplayArray(int *pArray, int nLen)
 
{
 
    for (int i = 0; i < nLen; ++i)
 
    {
 
        printf("array[%d] = %dn", i, pArray[i]);
 
    }
 
}
 
// pArray1和pArray2是已经排好序的数组,要求将它们按照顺序合并到pArray中
 
// 排序之后的数组不会有重复的元素
 
void MergeArray(int *pArray1, int nLen1, int *pArray2, int nLen2, int *pArray)
 
{
 
    int i, j, n;
 
    i = j = n = 0;
 
    while (i < nLen1 && j < nLen2)                 // 循环一直进行到拷贝完某一个数组的元素为止
 
    {
 
        if (pArray1[i] < pArray2[j])             // 拷贝array1的元素
 
        {
 
            pArray[n++] = pArray1[i++];
 
        }
 
        else if (pArray1[i] > pArray2[j])          // 拷贝array2的元素
 
        {
 
            pArray[n++] = pArray2[j++];                       
 
        }
 
        else                        // 相等的元素拷贝
 
        {
 
            pArray[n++] = pArray2[j++];                       
 
            ++i;
 
        }
 
    }
 
    if (i == nLen1)                 // 如果array1已经被拷贝完毕就拷贝array2的元素
 
    {
 
        while (j < nLen2)
 
            pArray[n++] = pArray2[j++];
 
    }
 
    else                         // 如果array2已经被拷贝完毕就拷贝array1的元素
 
    {
 
        while (i < nLen1)
 
            pArray[n++] = pArray1[i++];
 
    }
 
}
 
int main()
 
{       
 
    int array1[] = {1, 4, 5, 7};
 
    int array2[] = {2, 3, 6, 8};
 
    int array3[8];
 
    MergeArray(array1, 4, array2, 4, array3);
 
    printf("Merge Array:n");
 
    DisplayArray(array3, 8);
 
    return 1;
 
}


用C#写一段代码,使用递归算法从一个数组中找出最大值和最小值
class Program
    {
        public static int Max(int[] a, int b)
        {
            if (b < a.Length-1)
            {
                return a[b] < Max(a, b + 1) ? Max(a, b + 1) : a[b];
            }
            return a[b];
        }
        static void Main(string[] args)
        {
            int[] a ={ 3, 4, 5, 7, 8, 2, 1, 6, 7, 9 };
            Console.WriteLine(Max(a, 0));
            Console.ReadKey();
        }
    }
这个是求最小的,都是用控制台写的




C# code
using System;
using System.Collections.Generic;
using System.Text;


namespace arrayDiguiTest
{
    class Program
    {
        public static int Min(int[] a, int b)
        {
            if (b < a.Length-1)
            {
                return a[b] > Min(a, b + 1) ? Min(a, b + 1) : a[b];
            }
            return a[b];
        }
        static void Main(string[] args)
        {
            int[] a ={ 3, 4, 5, 7, 8, 2, 1, 6, 7, 9 };
            Console.WriteLine(Min(a, 0));
            Console.ReadKey();
        }
    }
}
C# 查找数组中指定数字,最小值,最大值。
 代码
 1   class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int searchNumber;
 6             bool found;
 7             //TestArray nums = new TestArray(10);
 8             int[] nums = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 9             #region  初始化数组
10             //Random rnd = new Random(100);
11             //for (int num = 0; num < 10; num++)
12             //{
13             //    nums.Insert(rnd.Next(0, 100));
14             //}
15             #endregion
16             Console.WriteLine("Enter a num to search for:");
17             searchNumber = Convert.ToInt32(Console.ReadLine());
18             found = NumSearch(nums,searchNumber);
19             if (found)
20             {
21                 Console.WriteLine(searchNumber + "is in the array.");
22             }
23             else
24             {
25                 Console.WriteLine(searchNumber + "is not in the array.");
26             }
27             //查找数组中最小值函数
28             int min=FindMin(nums);
29             int max = FindMax(nums);
30             Console.WriteLine(min.ToString());
31             Console.WriteLine(max.ToString());
32             #region  排序实现
33             //Console.WriteLine("Before Sorting: ");
34             //nums.DisplayElements();
35             //Console.WriteLine("Durring Sorting: ");
36             //nums.InsertionSort();
37             //Console.WriteLine("After Sorting: ");
38             //nums.DisplayElements();
39             #endregion
40             Console.ReadLine();
41         }
42         //<summary>
43         //查找数组中制定数字的方法
44         //<param name="arr">数组</param>
45         //<param name="value">数字</param>
46         //</summary>
47         //<returns>bool</returns>
48         public static bool NumSearch(int [] arr,int value)
49         {
50             for (int index = 0; index < arr.Length; index++)
51             {
52                 if (arr[index] == value)
53                 {
54                     return true;
55                 }
56             }
57             return false;
58         }
59         //<summary>
60         //查找数组中最小数字的方法
61         //<param name="arr">数组</param>
62         //</summary>
63         //<returns>bool</returns>
64         public static int FindMin(int[] arr)
65         {
66             int min = arr[0];
67             for (int index = 1; index < arr.Length; index++)
68             {
69                 if (arr[index] < min)
70                 {
71                     min=arr[index];
72                 }
73             }
74             return min;
75         }
76         //<summary>
77         //查找数组中最大数字的方法
78         //<param name="arr">数组</param>
79         //</summary>
80         //<returns>bool</returns>
81         public static int FindMax(int[] arr)
82         {
83             int max = arr[0];
84             for (int index = 1; index < arr.Length; index++)
85             {
86                 if (arr[index] > max)
87                 {
88                     max = arr[index];
89                 }
90             }
91             return max;
92         }
93 
94     }


 合并两个有序数组
算法思想:
        将两个有序数组合并为一个有序数组,也就是对两个数组中的所有元素进行排序。
        与一般排序所不同的是,各个数组都是排好序的,现在要做的是将各个排好序的数组进行归并,归并后,仍是有序的。
 
       要设计这个归并数组的算法,须先找出其中蕴含的规律:
       1) 对于同一个数组的各个元素,它们之间不用进行比较,因为它们是有序的
       2) 需比较大小的两个元素必定是分处于不同数组
      
算法流程:
    
     A数组的第i个元素与B数组的第j个元素进行比较
    // 比较到这一步, 说明A中已经有i个元素保存到C中,B中已经有j个元素保存在C中,目前,C中已经保存了i+j个元素, 因此,下一个比较的结果要放入C[i+j]单元中
 
    如果A[i]<B[j]         
             C[i+j]= A[i];
             i++;
     否则
             C[i+j]=B[j];   
             j++;
  
  然后,再重新比较A[i]和B[j]的大小,因此,上段程序是需要不断循环的,循环结束标志是 A中所有元素或B中所有元素都已经保存到了C中。
 
   while(A中还有未遍历元素&&B中也还有未遍历元素){
    如果A[i]<B[j]         
             C[i+j]= A[i];
             i++;
     否则
             C[i+j]=B[j];   
             j++;

   while(A中还有未遍历的元素)
            C[j+i++]=A[i++];
 
  while(B中还有未遍历的元素)
           C[i+j++]=B[j++];
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值